Labels

Wednesday, June 9, 2010

return value from printf() function

What is the return value from printf() function?


printf function always returns the number of characters printed. Let us understand this 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 value of a, three times with space between each value, 10 10 10. Total 5 characters get printed (3 value of 10 and 2 spaces).


As explained earlier the inner printf after printing the values, returns the number of characters printed, 5 which is printed by the outer printf.


The output of the above program is


10 10 10 5


A function always returns a value and printf function returns the number of characters successfully printed.

No comments:

Post a Comment