suClick
suClick

Reputation: 996

What's the return value of the ternary operation "?:"?

My classmate asked me a question:

What's the value of x,y,z after this:

x=5;
y=8;
z=((x++)<(y++)?(x++):(y++));

I 'm not sure about it, so I tested it and answer is:

x=7,y=9,z=6

I can understand why "x=7" and "y=9", but why "z=6" ? Shouldn't this expression return the value calculated by "x++" ?

Thanks for help :)

Upvotes: 2

Views: 156

Answers (1)

Pavan Manjunath
Pavan Manjunath

Reputation: 28525

There is a sequence point between the evaluation of the first operand of ? operator and second or third operator and hence first x++ would completely take effect resulting in x=6. But the increment in the second x++ would take effect only after the evaluation of the complete expression as there are no more sequence points in that expression and hence x remains 6 and z=6.

Upvotes: 7

Related Questions