simple operator<< question

Hello everbody!

I am wondering what is happening behind this code:

int x = 4;
cout << ++x << x++ <<endl;
int y = 4;
cout << y++ << ++y;


The output is:
64
56

Could someone explain what happens there?
I can understand the 64, but the 56 makes no sense for me.

Br,
Gr3go
Hi gr3go,

I'm no expert but I know this is nothing to do with the << operator. It is to with the order in which the line is assessed by the compiler, and the order that you increment x and y in your cout line.

if x=4:

cout << x++; would output 4, then raise the value of x to 5 afterwards.

cout << ++x; would raise the value of x to 5, then output 5.

I don't feel I did a great job of explaining that but someone else is bound to come along and do a better job =P

That depends on 2 things. First on your compiler, second on the order the arguments are pushed in your cout function. Depending on the amount of precompiled data you get different results.
MSVC for example will give you 54, 56 as a result.
This is because of the defined scope of x&y and so the compiler will precompute the values.
Even then 64, 56 is kinda strange.
Thanks for the quick answers :)
The undefined behavior seems to be a reasonable answer :)

jmc
In my previous post, the output was generated by an exe compuled with MSVC.
It was 64 56, not 54 56....
Tried with visual studio 2008
Topic archived. No new replies allowed.