C pre increment and post increment
-
Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?
-
Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?
-
Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?
It is executed in this order here:
// Pre-increment is executed first
++x; // 12
++x; // 13
// push first parameter on the stack: 13
push x;
// post increment
x++; // 14
// push 2nd and 3rd parameter on the stack: 14
push x;
push x;But see also Why does x = ++x + x++ give me the wrong answer?[^]
-
It is executed in this order here:
// Pre-increment is executed first
++x; // 12
++x; // 13
// push first parameter on the stack: 13
push x;
// post increment
x++; // 14
// push 2nd and 3rd parameter on the stack: 14
push x;
push x;But see also Why does x = ++x + x++ give me the wrong answer?[^]
Good explanation..Thanks
-
Because you should never use such expressions. The compiler writers are allowed to handle the order of incrementation in any way they like, so the results may or may not be what you expect. Bottom line: don't do it.
I agree. but my doubt was how it happens.
-
Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?
This expression is undefined by the C Standard, meaning that the compiler can do anything with it, including formatting your disk or causing demons to fly out of your nose. You should consider yourself lucky that the compiler only incremented 'x' in a weird manner. :) Note that even changing the compiler options (e.g. optimization) might change the printed results, so even if this "appears to work properly" on your system - DON'T DO IT! (On comp.std.c and comp.lang.c they used to refer to a notional system, the DeathStation 9000, that would actually create Nasal Demons when it encountered undefined expressions :) )
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
-
Good explanation..Thanks
It isn't a good answer it's wrong the result is undefined. Try it on VS2017 and you will likely get "13 13 13 " because of the way the optimizer works.
In vino veritas
-
This expression is undefined by the C Standard, meaning that the compiler can do anything with it, including formatting your disk or causing demons to fly out of your nose. You should consider yourself lucky that the compiler only incremented 'x' in a weird manner. :) Note that even changing the compiler options (e.g. optimization) might change the printed results, so even if this "appears to work properly" on your system - DON'T DO IT! (On comp.std.c and comp.lang.c they used to refer to a notional system, the DeathStation 9000, that would actually create Nasal Demons when it encountered undefined expressions :) )
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
thank you .. :)