Velocity Reviews > What is sequence point ?

# What is sequence point ?

sugaray
Guest
Posts: n/a

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

Joona I Palaste
Guest
Posts: n/a

 03-01-2004
sugaray <(E-Mail Removed)> scribbled the following:
> 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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"

Martin Dickopp
Guest
Posts: n/a

 03-01-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (sugaray) writes:

> Can somebody explain to me what is sequence point ?

Martin

--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/

Manish Singh
Guest
Posts: n/a

 03-01-2004
(E-Mail Removed) (sugaray) wrote in message news:<(E-Mail Removed). com>...
> 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