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
Why does this happen?
I was messing around with incrementing variables when I found this interesting tidbit:
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:
It should output:
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
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]
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]
You know what, I'd be willing to bet this is a problem with VC++6, gcc does it properly (0,0,1).
I wouldn't be suprised if it was either, or just a very unusual feature :) Anybody else have any ideas on it?
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement