Conditional operator

Please help me and tell the o/p of following program..

int i = 11;

i = (11 == i) ? i++ : i--;

cout<<"i : "<<i<<endl;

o/p is 11
but, as i=11 the condition will be true and it shud increement i to 12 and then print i as 12,
but it is giving as 11.

can anybody help me?

Thanks
I would assume that writing to the same variable twice within the same expression yield in "undefined behaviour" (or maybe "implementation-specific"). Same as i=(i=1)+1;.

I haven't looked in the C++ standard for this exactly, but I would not be surprised if it is..
It's undefined.
ok..but
if i = 10

i = (i = 1)+ 1 will give u 2.. and that's correct.

but in case of our example, if we use pre-increment i.e ++i then it gives 12
@kbw ... do u know the reason?
closed account (z05DSL3A)
You basically have two side effects on the same variable at one sequence point. The two side effects are assigning a value to i and incrementing i. The order in which this will happen is not defined in the standard so you may get different results with different compilers.

two logical sequences:
1) i is evaluated, then incremented and the evaluation is assigned to i: result 11
2) i is evaluated, the evaluation is assigned to i and then i is incremented: result 12

Edit:
Strange forum behavior, there were no replies when I posted, now there are four before this.
Last edited on
Suraj: "undefined behaviour" includes "normal behaviour that you might expect in some cases" as well as "slightly strange behaviour". ;)

The point is: The compiler is allowed to make a couple of instruction reordering and optimizations with your code that you might not expect from your "normal" gut feeling.

So taken
 
i = (11 == i) ? i++ : i--;


the compiler could decide that its faster for him to first assign the right hand value of the = operation to i and after this, do the increment operation on i++.

It is allowed to do such reordering, because it got the blessing from the C++ standard, that no expression writes on the same variable twice. (Or if you do write twice like in the current example, its "undefined behaviour", which means it is OK for the compiler to generate code that do the strange behaviour you see as "errorornous").

And that's the punch line why you should never allow "undefined behaviour" in your code, even if it "seems working". It could even change the behaviour when you twich a couple of compiler options (like using "optimize for size" instead of "optimize for speed"). It could even change behaviour when just compiled the same code multiple times (although that's rather rare. Indeterministic compiler behaviour is still a very seldom thing to see ;))

i = (i = 1)+ 1 will give u 2.. and that's correct.

Nope, it gives you "undefined behaviour", which may be 2 for your compiler in your current optimization settings on your current platform most of the times. But still it could lead to a crash and the compiler can still be labelled "working correct according to the C++ standard".
Last edited on
@ Grey Wolf and imi

Thanks a lot..for the help...let me know more about it if u know
Topic archived. No new replies allowed.