What is a sequence point? what is its significance?

Questions by pbchaudhari   answers by pbchaudhari

Showing Answers 1 - 2 of 2 Answers

ganesh.g

  • Sep 5th, 2007
 

A sequence point is loosely referred as the point at which an expression is evaluated and all the objects involved in that expression are assigned their fresh values, if any.

Example: 1. Between the statements i. printf("%d", *intp++) and
                                                           ii. printf("%d", *intp)
                Here int pointer variable is updated before the next statement, i.e., ii.

                2. At the operators &&, || and ?: , you can imagine there is a sequence point.

One important concept in C programming Language is:

       When an object is modified and inspected for, within a single sequence point, the operation is UNDEFINED.

Example: These are undefined:
                                1. a[i] = i++;
                                2. printf("%d t t %d nn", *intp++, *intp);
                                3. int i = 7;
                                    printf("%d n", i++ * i++ );


End Note : For this question, better reference is "C Programming FAQ" By Steve Summit, especially chapter titled: Expressions.

Any mistakes are my own.

Thanks
Ganesh.G,

Bangalore, India.



  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

A sequence point is a point in the flow of the program at which certain things have to be completed before the code can continue.

For example, suppose you had the following in your code:

   printf( "X=%d  Y=%dn", func_x(), func_y() );

What would the effect be if printf were called _before_ func_x and func_y were called, which is to say, before the parameters to printf were fully evaluated?  Chaos, most likely.

Fortunately, the language defines that there is a sequence point at the start of a function - meaning that all side-effects, evaluations, etc, must be completed before the function can actually commence.  In this case, it means that the printf functions parameters (func_x and func_y) have to already be called, have returned, and their results stored somehow, before actually invoking printf, which now uses those values.

There are other aspects of it, but that's the general idea: it's a point, in the code flow, where evaluations have to be evaluated, side-effects taken into effect and so forth, before continuing.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions