Macros do text substitution. Your code is equivalent to:
printf("%d %d %d
",a,b, a++ > b++ ? a++ : b++);
This has undefined behavior, because b
is potentially incremented (at the end of the third argument) and then used (in the second argument) without an intervening sequence point.
But as with any UB, if you stare at it for a while you might be able to come up with an explanation of what your implementation has actually done to yield the result you see. Order of evaluation of arguments is unspecified, but it looks to me as though the arguments have been evaluated in right-to-left order. So first, a
and b
are incremented once. a
is not greater than b
, so b
is incremented again and the result of the conditional expression is 5
(that is to say, b
after the first increment and before the second).
This behavior is not reliable - another implementation or the same implementation on another day might give different results due to evaluating the arguments in a different order, or theoretically might even crash because of the sequence point issue.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…