Sequence points.

Discussion in 'C++' started by kwrzalik@gmail.com, May 26, 2006.

  1. Guest

    Hello,
    I was solving a C++ quiz that has the following question:


    What is the outcome of running the following code:

    int c=0;
    cout<<c++<<c;

    The answer says undefined. But in this case, isn't this statement
    translated into:

    cout.operator<<(c++).operator<<(c);

    There should be a sequnce point before calling a funcion after
    evaluation of the arguments. So shouldn't the incremented value of c be
    stored before the call to first <<. If so it should be available for
    the second <<, and the result would be 01.

    Please help me with this confusion.
    , May 26, 2006
    #1
    1. Advertising

  2. benben Guest

    wrote:
    > Hello,
    > I was solving a C++ quiz that has the following question:
    >
    >
    > What is the outcome of running the following code:
    >
    > int c=0;
    > cout<<c++<<c;
    >
    > The answer says undefined. But in this case, isn't this statement
    > translated into:
    >
    > cout.operator<<(c++).operator<<(c);
    >
    > There should be a sequnce point before calling a funcion after
    > evaluation of the arguments. So shouldn't the incremented value of c be
    > stored before the call to first <<. If so it should be available for
    > the second <<, and the result would be 01.
    >
    > Please help me with this confusion.
    >


    Compilers have right to delay the increment operation on c until the end
    of the statement.

    Ben
    benben, May 26, 2006
    #2
    1. Advertising

  3. wrote:
    > Hello,
    > I was solving a C++ quiz that has the following question:
    >
    >
    > What is the outcome of running the following code:
    >
    > int c=0;
    > cout<<c++<<c;
    >
    > The answer says undefined. But in this case, isn't this statement
    > translated into:
    >
    > cout.operator<<(c++).operator<<(c);
    >
    > There should be a sequnce point before calling a funcion after
    > evaluation of the arguments. So shouldn't the incremented value of c be
    > stored before the call to first <<. If so it should be available for
    > the second <<, and the result would be 01.


    There is a sequence point between the function calls, but not between
    the evaluation of the arguments. That means, whether "c" is evaluated
    before or after "c++" is undefined. Don't do that.


    Jonathan
    Jonathan Mcdougall, May 26, 2006
    #3
  4. Guest

    Could you please elaborate on this more verbosely? I understand that if
    c++ is evaluated as an argument of a function and there's a sequence
    point bofore calling this function the increment operator must be
    executed and saved at this point. Because the dot operator bind from
    left to right, the c should be incremented and saved so that it is
    accessible in the second operarator<< call.
    , May 26, 2006
    #4
  5. Guest

    Are you sure it's the end of the function and not the so called
    sequence point?
    , May 26, 2006
    #5
  6. benben Guest

    wrote:
    > Are you sure it's the end of the function and not the so called
    > sequence point?
    >


    Negative...I'll go back and read the standard...

    Regards,
    Ben
    benben, May 26, 2006
    #6
  7. Kyku Guest

    You seem to be right. Thank you for your explanation. I didn't think
    about it that the two c's can be evaluated at any point.
    Kyku, May 26, 2006
    #7
  8. Rolf Magnus Guest

    wrote:

    > Could you please elaborate on this more verbosely? I understand that if
    > c++ is evaluated as an argument of a function and there's a sequence
    > point bofore calling this function the increment operator must be
    > executed and saved at this point.


    Yes, but the other one can be executed already, too. It doesn't have to be
    deferred to after the first function (or even after operator++) is called.
    The compiler is free to first evaluate c, then c++, then call the first
    operator<<, then the second one.
    At least that's how I understand it.
    Rolf Magnus, May 26, 2006
    #8
    1. Advertising

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. Dave Theese

    Sequence points

    Dave Theese, Sep 3, 2003, in forum: C++
    Replies:
    2
    Views:
    336
    Karl Heinz Buchegger
    Sep 3, 2003
  2. Timothy Madden

    Does C++ have sequence points ?

    Timothy Madden, Sep 20, 2004, in forum: C++
    Replies:
    4
    Views:
    405
    Peter van Merkerk
    Sep 21, 2004
  3. j

    Sequence points

    j, Jul 23, 2003, in forum: C Programming
    Replies:
    4
    Views:
    373
    Tak-Shing Chan
    Jul 24, 2003
  4. akarl

    Knowing your sequence points

    akarl, Aug 11, 2005, in forum: C Programming
    Replies:
    7
    Views:
    388
    Jack Klein
    Aug 12, 2005
  5. Question about sequence points

    , Aug 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    279
    Clark S. Cox III
    Aug 12, 2006
Loading...

Share This Page