• Have Any Question?
  • contact@eitworld.com

Printf and Scanf





printf and scanf function in C Programming

 

printf() function in C

  • printf() is an output function.
  • printf() function is used to display the data value or message on the output screen.
  • printf() function is a predefined function that is defined in the stdio.h header file.
  • This function requires format string and variable’s name to print the data value stored in variable. The format string and variables should be same in number. Here is a syntax to print data value stored in the variable through printf() function:
printf syntax in c:
printf(“format string”, variable_name);

Here is an example of printf() function:

Example

#include<stdio.h>

#include<conio.h>

int main()

{

int a=10;

float b=10.20;

char c='w';

printf("Display the value of a, b and c:");/*only print a simple text message*/

printf("na=%d,b=%f,c=%c",a,b,c);/*print the data value stored in variable*/

getch();

return 0;

}

Output

Output of the above program would be as follows:

Display the value of a,b and c:

a=10,b=10.20000,c=w

scanf() function in C

  • ◉ scanf() is an input function.
  • scanf() function is used to read all type of data value entered by the user.
  • It is basically used for runtime assignment of variables.
  • scanf() function is a predefined function that is defined in stdio.h header file.
  • scanf() function also requires format string to identify the data to be read at the time of execution of the program.
  • scanf() function also requires the ampersand (&) is used to get the “address” of the variable.
  • The syntax of scanf() function is as follows:
scanf syntax in c:
scanf(“format_string”,&variable_name);

Here is an example of scanf() function as follows:

Example

#include<stdio.h>

#include<conio.h>

int main()

{

int a;

printf("n Enter the value of a:");

scanf("%d",&a);

a=a*2;

printf("value of a after multiply by 2:%d",a);

getch();

return 0;

}

Output

Output of the above program would be as follows:

Enter the value of a:2

value of a after multiply by 2:4



Real Time Web Analytics