Sign in to follow this  
tombifkin

Why does this happen?

Recommended Posts

I was messing around with incrementing variables when I found this interesting tidbit:
int x=0;
cout << x << endl << x++ << endl;
cout << x << endl;
You would thing the output would be "0,0,1" but it's not. Anyone know why? I remember reading somewhere that the compiler reads right to left, but I couldn't find where. By the way, I'm using MSVC++ 6.0. Tom

Share this post


Link to post
Share on other sites
When the "++" is after the variable, it performs the incrementation after the line is executed. If it is before the variable via "++x" then the incrementation is done before the line is executed. Try this out:


int x=0;
cout << "x = " << x << endl;
cout << "x++ = " << x++ << endl;
cout << "x = " << x << endl;
cout << "++x = " << ++x << endl;


It should output:

x = 0
x++ = 0
x = 1
++x = 2

Share this post


Link to post
Share on other sites
Thanks for the replies so quickly.

The output is "1,0,1".

Rob:
I understand what you are saying. I know about the post/pre increments and how they differ, I'm just wondering why it gives me that output. I do not think I would ever have the need to write this ugly, I'm just wondering why it happens.

EDIT:
After reading through your(Rob's) post again, I get what you were saying. But what if you have, like in the code I posted above, two variables. On being displayed first then being increment later in the line. How does the compiler read that line? Does it read "x++" first and then display the value of x? Hopefully that makes sense. Thanks for the help so far!

I just found the post that I was talking about: Clicky
Go down to Nurgle's post.

[Edited by - tombifkin on December 4, 2004 11:10:29 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by JuNC
Ahhh, C++ - the language where even IO order can't be guaranteed. My kingdom for a monad ;P

It's about order of evaluation, not the order in which they are passed to the streams.

In the statement

cout << x << endl << x++ << endl;

there are two evaluations involving x; "x" and "x++". Which of the two is evaluated first is not defined by the specification. A compiler is free to evaluate them in any order. So if "x++" is evaluated first, then that expression evaluates to "0", and "x" is then evaluated to "1" becuase the first evaluation increased the value of x by one*. Or if the compiler chooses to evaluate them in the other order, "x" first evaluates to "0", and then "x++" evaluated to "0" aswell, and then increasing the value.

The order in which the values are passed to the streams are, of course, guaranteed. It's just the order in which the arguments value are evaluated that isn't guaranteed.

* I don't think the increase of the value is quaranteed to occur on evaluation, but as long as it's evaluated before the next sequence point. But let's assume it does increase on evaluation, just for sake of describing the behaviour here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this