Printf & scanf in C Language
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. It prints the given statement to the console.

The general form of printf() function is given below :

  Syntax :
printf("format string", list of variables);
  Example :
printf(“%d %d”,a,b);

The formatted string represents, what format we want to display on the screen with the list of variables. The format string can be following table :

Data Type Conversion format string
Integer short integer %d or % i
short unsigned % u
long signed % ld
long unsigned % lu
unsigned hexadecimal % x
unsigned octal %O
Real float % f or % g
double %lf
signed character %c
unsigned char %c
string %s
scanf() function :

This function is used to read values using keyboard. It is used for runtime assignment of variables.

The general form of scanf( ) is :

  Syntax :
scanf("format string", list_of_addresses_of_Variables ); 

The format string contains - Conversion specifications that begin with % sign

  Example :
scanf(“%d %f %c”, &a, &b, &c);

& ” is called the address operator, in scanf( ) the “ & ” operator indicates the memory location of the variable. So that the value read would be placed at that location.

Program : Let's see a simple example of C language that gets input from the user and prints the square of the given number.

void main()
int number;  

printf(“enter a number:”);  
printf(“square of number is:%d “,number*number);  
Output :
enter a number: 5
square of number is: 25

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

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

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

void main()
int x=0,y=0,result=0;  
printf(“Enter first number : ”);  
printf(“Enter second number : ”);  
printf(“Sum of 2 numbers : %d “,result);    
Output :
Enter first number : 9
Enter second number : 27
Sum of 2 numbers : 36

We see you’re using an ad blocker.

Our website is made possible by displaying ads to our visitors.

Please help us continue to provide you with free. So please disabling your ad blocker.