By Zshn


2012-06-12 11:09:14 8 Comments

I've an expression of the form shown below :-

while (count)
{
...
...

    index = ((count == 20)? 0 : index++);
...
...
}

Now Ternary operators are sequence points in C but I believe that the sequence point ends at the test part.

Is this understanding correct and as such will this statement lead to undefined behaviour ?

1 comments

@Daniel Fischer 2012-06-12 11:13:19

Right. There's a sequence point after the evaluation of the condition, but the next sequence point is the semicolon terminating the statement. So whenever count != 20, you have the undefined behaviour

index = index++;

since index is modified twice without intervening sequence point.

@Lundin 2012-06-12 11:53:49

And the opposite: index = (index++ == 20) ? 0 : something; is well-defined behavior (although poor programming practice).

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

5 Answered Questions

[SOLVED] Undefined behavior and sequence points

31 Answered Questions

[SOLVED] How do I detect unsigned integer multiply overflow?

4 Answered Questions

[SOLVED] What does the C ??!??! operator do?

  • 2011-10-19 16:56:59
  • Peter Olson
  • 249607 View
  • 1892 Score
  • 4 Answer
  • Tags:   c operators trigraphs

3 Answered Questions

[SOLVED] What is the difference between a sequence point and operator precedence?

0 Answered Questions

Sequence point in the assignment operator

2 Answered Questions

[SOLVED] Does this contain a sequence point?

  • 2015-10-12 17:03:05
  • Paul Ogilvie
  • 68 View
  • 2 Score
  • 2 Answer
  • Tags:   c sequence-points

1 Answered Questions

[SOLVED] sequence point and side effects

5 Answered Questions

[SOLVED] Undefined behavior and sequence points reloaded

6 Answered Questions

[SOLVED] Sequence Points vs Operator Precedence

Sponsored Content