Jump to content
  • Advertisement
Sign in to follow this  
tombifkin

Why does this happen?

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
You know what, I'd be willing to bet this is a problem with VC++6, gcc does it properly (0,0,1).

Share this post


Link to post
Share on other sites
The insertion operator is not a sequence point, so the evaluation of x++ and x can occur at any time during that statement, meaning the results are non-deterministic.

Share this post


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

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
Ah, thanks for the explanation. It makes sense now. It's always nice to learn something new. Thanks for all of the replies!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!