What is sequence point ?

Discussion in 'C Programming' started by sugaray, Mar 1, 2004.

  1. sugaray

    sugaray Guest

    Can somebody explain to me what is sequence point ?
    With few examples would be even better. Thanx for your help.
    sugaray, Mar 1, 2004
    1. Advertisements

  2. sugaray <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
    - Guardian in "Jinxter"
    Joona I Palaste, Mar 1, 2004
    1. Advertisements

  3. (sugaray) writes:

    > Can somebody explain to me what is sequence point ?

    Read the FAQ, please. (Q3.8)


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

    Manish Singh Guest

    (sugaray) wrote in message news:<>...
    > 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);

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

    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.

    Manish Singh
    Manish Singh, Mar 1, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sensorflo
    Ron Natalie
    Aug 19, 2004
  2. Dave

    Sequence point problem?

    Dave, Feb 5, 2005, in forum: C++
    Victor Bazarov
    Feb 7, 2005
  3. Deniz Bahar

    What is a sequence point?

    Deniz Bahar, Feb 11, 2005, in forum: C Programming
    Tim Rentsch
    Feb 17, 2005
  4. stef mientki
    stef mientki
    Oct 20, 2007
  5. Saraswati lakki
    Saraswati lakki
    Jan 6, 2012

Share This Page