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,defined in stdio.h (header file).

printf( ) function

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

The syntax of printf( ) function is given below:

  • printf("format string",argument_list);
  • The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

One example use printf()

#include< stdio.h >
void main( )
{
printf("welcome to Tutorial Zone");
}

Output:

welcome to Tutorial Zone

scanf( ) function

  • The scanf() function It reads the input data from the console.

The syntax of scanf() function is given below:

  • scanf("format string",&variable_name);
  • The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

One example use scanf( )

#include< stdio.h >
void main( )
{
int num;
printf("Enter a number:");
scanf("%d",&num);
}

Output:

Enter a number:10

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

#include< stdio.h
void main()
{
int a,b,sum;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
sum=a+b;
printf("\n sum of two number is :%d",sum);
}

Output:

Enter first number:10
Enter second number:10
sum of two number is:20