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

0

need help with post and pre increments

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

c

3/19/2020 4:45:49 PM

Abdelali Khademallah

2 Answers

New Answer

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