Home »
C programming language
Returned values of printf and scanf in C language
Return values of printf and scanf in C: Here, we will learn what printf and scanf return after execution?
1) Return type and value of printf function
printf is a library function of stdio.h, it is used to display messages as well as values on the standard output device (monitor).
printf returns an integer value, which is the total number of printed characters.
For example: if you are printing "Hello" using printf, printf will return 5.
Consider the program:
#include <stdio.h>
int main()
{
int result;
result = printf("Hello\n");
printf("Total printed characters are: %d\n",result);
return 0;
}
Output
Hello
Total printed characters are: 6
In first statement "Hello\n" there are 6 characters, "\n" is a single character to print new line, it is called new line character. Thus, total number of printed characters are: 6.
2) Return type and value of scanf
scanf is a library function of stdio.h, it is used to take input from the standard input device (keyboard).
scanf returns an integer value, which is the total number of inputs.
For example: if you are reading two values from scanf, it will return 2.
Consider the program:
#include <stdio.h>
int main()
{
int a,b;
int result;
printf("Enter two number: ");
result=scanf("%d%d",&a,&b);
printf("Total inputs are: %d\n",result);
return 0;
}
Output
Enter two number: 10 20
Total inputs are: 2
Here, we are taking two integer values 10 and 20 as input, thus, scanf() will return 2. Thus, the total inputs are 2.
These programs may help you to understand the concept of return type of printf and scanf...
Program 1)
int main()
{
printf("%x\n","Hello");
return 0;
}
Output
Some memory address, which will a temporary memory address where "Hello" will be stored.
Program 2)
int main()
{
int result;
result = printf("Hello\n");
printf("%d\n",result);
return 0;
}
Output
Hello
6
Statement printf("Hello\n"); will print "Hello" and a new line character (which is "\n") and printf will return 6, (printf returns the total number of printed characters).
C Language Tutorial »