+ 1
i=0 cout<< i++ << ++i << i; why the output is 122...shouldnt it be 022.?
i think output should be 022...but why 1 is there?
4 Antworten
+ 5
It's strage isn't it. I think because they are in same cout. if you write it on multy cout like this:
int i=0;
cout<< i++ ;
cout<< ++i ;
cout<< i;
it will work fine.
+ 2
What @Mohamed Aleid said is correct. Because i, i++, and ++i are all on the same statement, it's still going to run the pre-incrementations before anything else.
To prove this,
cout << i++ << ++i << ++i;
This would output 233, where as logically, you'd think the output would be 023.
It's just how the compiler does it.
+ 1
122 is correct . because;
i++ one out = 0 but i=1
++i = 1+1 = 2 and i = 2
so; cout <<1 <<2 <<2;
0
the output will be 110 because in c++ the compiler calculates from right to left (in case of cascading of <<) and prints from left to right...:-)