0

I have the following code:

#include <stdio.h>

int main() 
{
    int a = 2, c;
    c = a++ + a;
    printf("%d\n", c); // prints: 5
}

I thought this would print 4 because the side effect of ++ shouldn't evaluate until the sequence point ;. However, 5 is printed. I then thought: it must be that + is a not-very-famous sequence point.


To confirm my previous theory (that + is a seq. point), I've changed one line:

c = a + ++a; // prints: 6

I thought this would print 5 as the previous case. However, this prints 6. I've come to the conclusion that I have no idea what's going on here. What am I missing (seems like a lot)?

Compiler version (is this even compiler-dependent?): gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

PajLe
  • 791
  • 1
  • 7
  • 21
  • 3
    No, [it's undefined behavior](https://stackoverflow.com/q/949433/10077). – Fred Larson Mar 13 '20 at 21:16
  • 1
    no and this expression is an UB – 0___________ Mar 13 '20 at 21:16
  • I was thinking to add "is this even a defined behaviour" to the question. Well, I guess that solves it. Thanks! – PajLe Mar 13 '20 at 21:19
  • 1
    'Annex C' of the C standard describes what constitutes Sequence Points. – Christian Gibbons Mar 13 '20 at 21:22
  • 2
    Is it worth considering that this question may not be a duplicate as it is specifically asking whether the `+` operator is a sequence point, rather than being a generic question about pre/post increment/decrement operators resulting in undefined behavior? It may be worthy of an answer that focuses more on sequence points than the accepted answer to the one of which it is marked a duplicate. – Christian Gibbons Mar 13 '20 at 21:28
  • 3
    Your reasoning seems to be that the side effect of `++` does not occur until a sequence point, and therefore the fact that it appeared to occur earlier than the `;` was reached implies something before that is a sequence point. This uses a false premise: The C standard does **not** say that side effects are executed just before or at a sequence point. It merely says that, at a sequence point, all side effects (and value computations) associated with the expression prior to the sequence point are done. They could have been done in **any** order prior to that (subject to other rules). – Eric Postpischil Mar 13 '20 at 21:33
  • Nice @Eric . I interpreted your words "are done" like: "are finished". So if I understood correctly, side effects are done in some order, but they must all be finished at the sequence point's occurrence (if they didn't already finish before sp occurrence)? – PajLe Mar 13 '20 at 21:44
  • 1
    Yes. In the words of the C 2018 standard, 5.1.2.3 3: “… The presence of a *sequence point* between the evaluation of expressions *A* and *B* implies that every value computation and side effect associated with *A* is sequenced before every value computation and side effect associated with *B*.” A sequence point is a hard barrier between things before and things after. There are also rules in the C standard about weaker sequencing relationships in various circumstances. – Eric Postpischil Mar 13 '20 at 23:30
  • Awesome, thanks @Eric ! – PajLe Mar 14 '20 at 00:04

0 Answers0