There are a number of undefined bits of the puzzle, but printf
is the key to the output. Others have detailed the illegality of the i+main(++i)
expression, so let's just look at the printf
to analyze the output:
printf(" %d\n",printf("%d",i+main(++i)));
There are two printf
statements, where the printf("%d",i+main(++i))
must be evaluated first in order to provide output to the other. So choose your undefined value and plug that in the %d
. From here on out the behavior is defined.
man printf
- Returned value:
Upon successful return, these functions return the number of characters printed
Given that printf("%d",i+main(++i))
outputs 10
in the first case, the return is 2
for the two characters printed. That value of 2
is passed to printf(" %d\n",..
giving the output for the first pass as:
10 2
Choose your undefined behavior on the second pass and the value for i+main(++i)
is 9
. The return for printf("%d",i+main(++i))
is now 1
giving your second line of output:
9 1
And so on and so forth... I cannot explain the undefined 10, 9, 8, ...
but the rest makes sense.