printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions. printf() is used to display the output and scanf() is used to read the inputs.

printf() and scanf() functions are declared in “stdio.h” header file in C library. The printf() and scanf() functions are commonly used functions in C Language. 

We have to include “stdio.h” file in C program to make use of these printf() and scanf() library functions in C language.

printf() function

The printf() function is used for output. It prints the given statement to the console.

In C programming, printf() is one of the main output function. It sends formatted output to the screen.


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. In C programming, scanf() is one of the commonly used function to take input from the user.


scanf("format string",argument_list);  


/*Let's see a simple example of c language that gets input from the user and prints the cube of the given number.
int main(){    
int number;    
printf("enter a number:");    
printf("cube of number is:%d ",number*number*number);    
return 0;  
C Program to print cube of given number – Output

The printf(“cube of number is:%d “,number*number*number) statement prints the cube of number on the console.

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

Example – 2

/*Let's see a simple example of input and output in C language that prints addition of 2 numbers.*/

int main(){    
int x=0,y=0,result=0;  
printf("enter first number:");  
printf("enter second number:");  
printf("sum of 2 numbers:%d ",result);  
return 0;  
C Program to print sum of 2 numbers – Output