0

need help with post and pre increments

why a and c have different outputs? https://code.sololearn.com/cdR20jwuI369/?ref=app

19th Mar 2020, 4:45 PM
Abdelali Khademallah
Abdelali Khademallah - avatar
2 Réponses
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
19th Mar 2020, 5:39 PM
MO ELomari
MO ELomari - avatar
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
19th Mar 2020, 5:27 PM
Dennis
Dennis - avatar