What is sequence point ?

S

sugaray

Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.
 
J

Joona I Palaste

sugaray said:
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.

A sequence point is a point of time in the evaluation of a C expression,
where all side effects are guaranteed to have taken place.
Modifying the same variable twice, or modifying a variable and using its
old value for anything other than computing the new value, without a
sequence point in between, causes undefine behaviour.
If I remember correctly, sequence points include:
- The , (comma) and ?: (conditional) operators,
- The && and || operators,
- The semicolon at the end of a statement, and
- The entry point to a function, but this only concerns expressions
within that function.
Most importantly, the assignment operators (such as =), the pre-
and post- increment and decrement operators (++ and --), and the
comma *separator* in parameter lists are NOT sequence points.
 
M

Manish Singh

Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.

Joona has already explained the sequence points and its basic workings.
However, one thing he missed is the test condition of the while loop.
For a better cover of the sequence points, side effects and full expressions,
you need to consult a good book and/or C-FAQs.

As you asked for examples, here is one including a while loop sequence point.

#include <stdio.h>
int main(void)
{
int i;

for(i = 1; i <= 10; i++)
printf("%d\n", i);
printf("\n");

i = 1;
while(i++ <= 10)
printf("%d\n", i);
printf("\n");

return 0;
}

As you can see, in the for construct i is initialized to 1, then tested with
the value 10 and finally incremented after executing the printf() statement.
I don't think there is a need to explain the workings of the for loop, is it?
It normally prints the value of i from 1 to 10 on your output device, along
with newlines.

Then, we reset i to 1.
Notice that the while loop can be made to imitate the for loop, quite easily.
It appears that i will get incremented only after the printf() has done it job.
This way, the while loop should also print the values from 1 to 10, after all
this is what post-increment operator does, isn't it?

Not quite so. The while loop test condition serves as a sequence point. So,
in this case, i gets incremented immediately before the printf() receives
control. Side effect of this condition test is that i will get incremented
before the program reaches printf() statement. In the end, values from 2 to
11 will be printed on your output device.

But, what if you change the postfix form of i to prefix form? It still won't
help too much. It will only stop incrementing i after comparing it with 10,
thus resulting in printing of values 2 to 10.

HTH,
Manish Singh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top