0
need help with post and pre increments
why a and c have different outputs? https://code.sololearn.com/cdR20jwuI369/?ref=app
2 Answers
0
changing a variable twice without an intervening sequence point is one example of undefined behaviour.
the "b++ + ++b" expression causes an undefined behavior, as "b" is modified twice within a single sequence point (the "+" operator not constituting a sequence point). // same for the expression "++d + d++"
GCC â https://godbolt.org/z/jJjYqf
CLANG â https://godbolt.org/z/3P5_52
sequence points â https://en.wikipedia.org/wiki/Sequence_point
0
Because this is undefined behaviour, if you run the code you should get a warning.
"warning: operation on 'b' may be undefined [-Wsequence-point]"
ditto for 'a' .
clang just happens to get 4 for both, gcc doesn't, hence, undefined behaviour.
The + operator does not have a sequence point defined, so no one can say for sure whether the left- or the right hand side is evaluated first.
https://en.wikipedia.org/wiki/Sequence_point