Angus Angus - 3 months ago 24
C Question

Return value of printf() function in C

The

printf()
function will return the number of characters printed. But in the code below
why is it printing 5.

int a=1000;
printf("%d",printf("\n%d",a));


It prints "1000" once and a space, so altogether we have 2 characters.

It should output "1000 2". But its outputting "1000 5".

Mat Mat
Answer

The number of characters output is 5. 1000 is four characters. \n is one character.

printf doesn't return the number of "items" output like the scanf family of functions do for input. It returns the actual character count.