470,833 Members | 1,373 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Sequence points.

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 '06 #1
7 1537
kw******@gmail.com 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
May 26 '06 #2
kwrza...@gmail.com 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

May 26 '06 #3
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 '06 #4
Are you sure it's the end of the function and not the so called
sequence point?

May 26 '06 #5
kw******@gmail.com 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.

May 26 '06 #6
kw******@gmail.com 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
May 26 '06 #7
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.

May 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Theese | last post: by
3 posts views Thread by Sensorflo | last post: by
4 posts views Thread by Timothy Madden | last post: by
53 posts views Thread by Deniz Bahar | last post: by
7 posts views Thread by akarl | last post: by
9 posts views Thread by John Smith | last post: by
1 post views Thread by lovecreatesbea... | last post: by
4 posts views Thread by Daniel Kraft | last post: by
7 posts views Thread by Jrdman | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.