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).






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.