Code:#include<stdio.h> int main() { int a=2,b=3; a=a + b - (b=a); printf("%d %d",a,b); return 0; }
Code:#include<stdio.h> int main() { int a=2,b=3; a=a + b - (b=a); printf("%d %d",a,b); return 0; }
You can find out the output yourself by compiling and running the program. I would have expected you to have done that first, found that something was amiss, and then ask why.Originally Posted by ackr1201
As for why: the answer is related to the explanation given for the question in the recent thread post and pre increment operator
. In short, you are observing the effects of undefined behaviour because b is modified when it is also accessed for its value other than for the modification.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Who teaches this rubbish!?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
@laserlight: Thx for the suggestion....
My question is that why the output behavior is undefined?
I am thinking that parenthesis is executed first always(as it has highest priority). If it is so then it will give unique answer on all compilers but in reality its not happening(as i know answer is undefined). This is my confusion.
plz help me!!
> Exactly... and how do we get him to stop publishing the URL for these forums?
No, the so-called teachers over there NEED to come and visit, so they can actually get a freaking clue about what they're trying to teach.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.