What is the return value from printf() function?

printf function always returns the number of characters printed by the printf function. Let us see this in brief with an example:



main()

{

int a=10;

printf(“%d,printf(“%d %d %d,a,a,a));

}



In this above program the inner printf is first called which prints three times the value of a, with space between each value namely 10 10 10. So 5 characters namely 3 value of a namely 10 and 2 spaces which totals to 5 characters gets printed.


So as explained before the inner printf after printing the values returns the number of characters printed namely 5 which is printed by the outer printf .


The output of the above program is



10 10 10 5



So it is always that function returns a value and so is printf which returns the number of characters successfully printed.

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 16 of 16 Answers

achal mehra

  • Dec 5th, 2006
 

the printf() function returns the no. of characters to be printed.for e.g:

 i=printf("achal mehra");

 printf("%d",i);

           will give result as: i=11

  Was this answer useful?  Yes

anjanadevi

  • Dec 14th, 2006
 

h=printf("a");

printf("%d",h);

this will return the ascii value of a

  Was this answer useful?  Yes

Vishal Singh Bhardvaj

  • Aug 21st, 2007
 

printf function always returns the number of characters printed by the printf function. Let us see this in brief with an example:

main()
{
int a=10;
printf(“%d,printf(“%d%d%d,a,a,a));
}

In this above program the inner printf is first called which prints three times the value of a, with space between each value namely 10 10 10. So 6 characters(if a=100 then it will return 9 since 100 three times constitute 39 characters) totals 6 characters gets printed.

If we are giving the spaces between the %d in the printf statement then this space will also will be calculated.
So as explained before the inner printf after printing the values returns the number of characters printed namely 6 which is printed by the outer printf .

The output of the above program is

1010106

So it is always that function returns a value and so is printf which returns the number of characters successfully printed.

if the program is like this

main()
{
int a=10;
printf(“ %d,printf(“%d %d %d,a,a,a)); //Spaces are considered
}
Then it will print 10 10 10 8 

  Was this answer useful?  Yes

It is the number of characters what it return, in above example

int a=10;
printf("%d,(printf("%d%d%d",a,a,a))");
Output: 10 10 106
Since C compiler ignores spaces and since there are total 6 characters hence 6 is in the output.

 

  Was this answer useful?  Yes

Ayushee

  • Jul 12th, 2011
 

Code
  1. #include<stdio.h>

  2. int main()

  3. {

  4. int a=10;

  5. printf("%d",printf("%d%d%d",a,a,a));

  6. printf("/n");

  7. printf("%d",printf("%d %d %d",a,a,a));

  8. return 0;

  9. }

anwer would be 1010106
10 10 108

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions