need help with post and pre increments | SoloLearn: Learn to code for FREE!


need help with post and pre increments

why a and c have different outputs?


3/19/2020 4:45:49 PM

Abdelali Khademallah

2 Answers

New Answer


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 → CLANG → sequence points →


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.