By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,504 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,504 IT Pros & Developers. It's quick & easy.

What is sequence point ?

P: n/a
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sugaray <ru****@sohu.com> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
Nov 14 '05 #2

P: n/a
ru****@sohu.com (sugaray) writes:
Can somebody explain to me what is sequence point ?


Read the FAQ, please. (Q3.8)

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

P: n/a
ru****@sohu.com (sugaray) wrote in message news:<ad**************************@posting.google. 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
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.