printf and scanf

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

  
printf() function

The printf() function is used for output.

The syntax of printf() function is  
  
printf("format string",argument_list);
 
 The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
 
scanf() function

The scanf() function is used for input. It reads the input data from the console.

 scanf("format string",argument_list);  
 
 
Lets take an example
 
    #include<stdio.h>    
    int main(){    
    int a=0,b=0,result=0;  
      
    printf("Enter first number:");  
    scanf("%d",&a);  
    printf("Enter second number:");  
    scanf("%d",&b);  
      
    result=a+b;  
    printf("Sum of two numbers:%d ",result);  
      
    return 0;  
    }   
 

The scanf("%d",&a) statement reads integer number from the console and stores the given value in number variable.

The scanf("%d",&b) statement reads integer number from the console and stores the given value in number variable.

The printf("Sum of two numbers:%d ",result) statement prints the addition of two numbers on the console.

 

Output


 

 


 


No comments:

Post a Comment