TheNemesis584 TheNemesis584 - 1 month ago 13
C Question

Strange output from nested printf statemenets

Ok, I'm playing a bit with a code, trying do understand some tricks and how does it work, so I don't understand output of this code

int i = 8;
printf("%d", printf("%o", i));


result of this is 102, I don't know how, I know that 8 in octal system is 10, but what most confuses me is when I put space after
%o
like this

printf("%d", printf("%o ", i));


now, result is 10 3, what is going on here?

Aus Aus
Answer

Printf prints to standard out, and returns int, the count of printed characters. So you get:

10 3

which is:

10 is the evaluated inner printf that prints octal 8.

and 3, the evaluated outer printf that prints the "return" value of the inner printf = 3 printed chars.