-2

At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

Can anyone explain these lines and related terminologies in a beginners words?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    possible duplicate of [Undefined behavior and sequence points](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – timrau Jul 15 '15 at 01:40
  • Note that C++11 and C++14 do not have sequence points any more. It is a good idea to identify where you get your quotes from. – Jonathan Leffler Jul 15 '15 at 02:38
  • See [this answer](http://c-faq.com/expr/seqpoints.html) in the [C FAQ list](http://c-faq.com/). – Steve Summit Jul 15 '15 at 03:00
  • Note that the principal (most up-voted) answers for the proposed duplicate are only strictly valid for C++. Some of the examples, especially in the C++11 answer, are not applicable to C (or, more accurately, examples which are allowed and well-defined behaviour in C++ are not allowed and hence semantically invalid behaviour in C — even C11). This question is dual-tagged — that makes it very difficult to answer sanely. – Jonathan Leffler Jul 15 '15 at 03:08

2 Answers2

3

Expressions such as n++ have side effects, i.e. they not only produce a result, but also modify a variable.

The * operator does not introduce a sequence point. Therefore, in the expression n++ * n-- it is not specified, whether the side effect of n++ (incrementing n) has already happened when n-- is evaluated. Depending on that, n++ * n-- yields different results.

; introduces a sequence point. If n == 5, then after n++; n--;, n == 5 holds again.

Oswald
  • 31,254
  • 3
  • 43
  • 68
0

Consider the following statement:

x = y++ * --z;

There are three evaluations: y++, --z, and the result of y++ multiplied by the result of --z. There are three side effects: add 1 to y, subtract 1 from z, and assign the result of y++ * --z to x.

The sequence point is the point in the program's execution where all of the above operations are complete, which in this case is at the end of the statement.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
John Bode
  • 119,563
  • 19
  • 122
  • 198