Although you've already gotten several answers, I want to provide one more, because your question actually contained three separate misunderstandings, and I want to touch on all of them.
First of all, sizeof
is a special operator which, by definition, does not evaluate its argument (that is, whatever subexpression it's taking the size of). So sizeof(++a)
does not increment a
. And sizeof(x = 5)
would not assign 5 to x
. And sizeof(printf("Hello!"))
would not print "Hello".
Second, if we got rid of the sizeof
, and simply wrote
printf("%d %d", ++a, a);
we would not be able to use precedence to figure out the behavior. Precedence is an important concept, but in general it does not help you figure out the behavior of confusing operations involving ++
.
Finally, the perhaps surprising answer is that if you write
printf("%d %d", ++a, a);
it is not possible to figure out what it will do at all. It's basically undefined. (Specifically: in any function call like printf("%d %d", x, y)
it's unspecified which order the arguments get evaluated in, so you don't know whether x
or y
gets evaluated first -- although there is an order. But then, when one of them is a
and one of them is ++a
, you have a situation where a
is both being modified and having its value used, so there's no way to know whether the old or the new value gets used, and this makes the expression undefined. See this question for more on this issue.)
P.S. One more issue I forgot to mention, as noted by @Vlad from Moscow: %ld
is not a reliable way to print the result of sizeof
, which is a value of type size_t
. You should use %zu
if you can, or %u
after casting to (unsigned)
if you can't.