0

Compiler used: gcc 8.2

Command line: -Wall

My current understanding of a sequence point violation is code that somehow depends on the order of evaluation of operands/sub-expressions in a given expression. This is so because the order of evaluation of operands within an expression is unspecified, as noted here. Thus, code like:

a = 5;
b = a + ++a;

is a violation and caught by -Wsequence-point as there is an ambiguity in the result, i.e. should it be (5 + 6) or (6 + 6) ? I think there is a similar ambiguity in below code as we cannot know if the the 2nd ++a will be evaluated before the 1st:

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

int use() 
{ 
    int min;
    int a = 4, b = 5;
    min = MIN(++a, b);
    //min = ((++a) < b) ? (++a) : b;
    return min;
}

I am obviously missing something as this code does not warn me on -Wseqeuence-point. Can someone please help me understand what? Note that I have deliberately defined MIN as it is.

Iwillnotexist Idonotexist
  • 13,297
  • 4
  • 43
  • 66
  • 3
    There is a sequence point between a ternary operator’s predicate evaluation and either of its alternatives. – Iwillnotexist Idonotexist Nov 24 '18 at 03:04
  • @IwillnotexistIdonotexist Cool, I did not know that. I can accept this if you want to write this as an answer. Please include a link to a source that says this too in your answer. – abjoshi - Reinstate Monica Nov 24 '18 at 03:07
  • 2
    While the sequence point rules aren't always logical, this one is. The implementation needs first know which of the two alternatives to evaluate and it must fully evaluate one and not the other. This is not the case with operators like `+` where is there is no particular reason either should be evaluated first. – David Schwartz Nov 24 '18 at 03:39
  • Anyway I consider this as poor and confusing coding style. – ulix Nov 24 '18 at 04:36
  • 1
    @ulix How so? The above is a correct implementation of a MIN macro. This is one of the gotchas while using is such a macro, not using expressions with side-effects. – abjoshi - Reinstate Monica Nov 24 '18 at 04:41
  • @IwillnotexistIdonotexist I am relatively new here. Mind telling me what's wrong with the question(not implying that you downvoted) so that I won't repeat it. – abjoshi - Reinstate Monica Nov 24 '18 at 20:18
  • @abjoshi I’m currently the only upvote :-) I’m assuming the downvote is by someone who thought you might have found your answer in one of the Q&As already on SO. – Iwillnotexist Idonotexist Nov 24 '18 at 21:07

1 Answers1

5

Per the ISO C11 Standard, there exists a sequence point between the evaluation of the ternary/conditional operator's predicate and either of its alternatives, whichever one is evaluated. Therefore, the two ++a are sequenced with respect to each other by the intervening sequence point.

§6.5.15 Conditional operator

Syntax

1.

  conditional-expression:
      logical-OR-expression
      logical-OR-expression ? expression : conditional-expression

[...]

Semantics

  1. The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.
Iwillnotexist Idonotexist
  • 13,297
  • 4
  • 43
  • 66