Search code examples
cundefined-behavior

About sequence point and UB


Here is a question I can not explain clearly.


Which option is wrong and why?

(A) a += (a++);   
(B) a += (++a);
(C) (a++) += a;
(D) (++a) += (a++);

What is the difference between A and B?

My understand: A is a UB but B is OK because the side effect of ++a will be completed before the assignment. Is that right?

Update: What is the difference between ++a and a++ within the sequence point? The side effect of pre-increment(decrement) may be completed any time before the next seq-point just like post-increment(decrement)?


Solution

  • Which option is wrong and why?

    All of them are wrong, and that's because the first two invoke undefined behavior, and the last two don't compile. (And if they did, they would invoke UB as well.)