I don't agree: since the behaviour is undefined, there is no such thing as a "wrong" answer any more than there is a "right" answer.
You can play games, guessing how different implementations might handle it, but compilers are not required to give answers consistent with your guesses about specific implementations. (No matter how reasonable your assumptions seem to you.)
The point of the exercise is not to decide which is the more nearly correct answer, but to caution programmers that operators or functions with side effects can give results that mislead you.
Note that there is nothing illegal about the syntax of any of the statements, but they are symantic nonsense, since the C standard is very explicit that order of evaluation of all functions and of certain operators (including ++) is undefined.
Here's a simple rule of thumb. If you see an expression with ++ or -- applied more than once to the same variable, raise the red flag: change it!!!
Regards,
Dave
|