Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
In Java the evaluation order is specified to be left-to-right. Is this the case for C and C++ as well, or is it implementation dependent? I do remember that the evaluation order is unspecified for function arguments, but what about sub-expressions?
It is unspecified which of the arguments to
+is evaluated first – but that doesn’t even matter, because in C and C++, modifying the same object twice without an intervening sequence point is completely undefined behaviour.Here you’re modifying
xthree times without an intervening sequence point, so you’re well into here be dragonnes territory 😉The relevant part of the C99 standard is “6.5 Expressions”:
and
It’s possible to write legal code that demonstrates the unspecified order of evaluation – for example:
(It is unspecified whether you get output of
foobarorbarfoo).