Possible Duplicate:
Undefined Behavior and Sequence Points
int a=10;
a=a++;
a=++a;
a=(a++);
Can you guys please explain to me why none of these cases works?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In answer to the C++ question for this code at http://codepad.org/cYXEuRuQ
when compiled prints
This is a warning stating that the operation used is undefined and should not be used if possible. This is because in C++ the order of evaluation of
++relative to other expressions is not defined and not the same across all compilers. (Generally it doesn’t matter and is not a problem except in edge cases like these)The web site goes further and treats warnings as errors and does not run the code.
If you translate to Java it prints
as expected. What do you mean by “doesn’t work”?
The behaviour is defined in Java, but as LucTouraille points out its not defined in C++ so you can’t expect a particular behaviour which is the same for all compilers.
Another example.
it is the same as