Public Group

# Why does this happen?

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

## 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 on other sites
what is the output?

##### 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 = 0x++ = 0x = 1++x = 2

##### 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 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 on other sites
I wouldn't be suprised if it was either, or just a very unusual feature :) Anybody else have any ideas on it?

##### 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 on other sites
Ahhh, C++ - the language where even IO order can't be guaranteed. My kingdom for a monad ;P

##### Share on other sites
Quote:
 Original post by JuNCAhhh, 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 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!

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 28
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999916
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!