i=0 cout<< i++ << ++i << i; why the output is 122...shouldnt it be 022.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

18th Sep 2016, 7:26 PM
Himanshu Sharma
Himanshu Sharma - avatar
4 Answers
+ 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.
18th Sep 2016, 10:24 PM
Mohammed Chetehouna
+ 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.
19th Sep 2016, 12:26 AM
Cohen Creber
Cohen Creber - avatar
+ 1
122 is correct . because; i++ one out = 0 but i=1 ++i = 1+1 = 2 and i = 2 so; cout <<1 <<2 <<2;
18th Sep 2016, 10:29 PM
Sezgin GÜL
Sezgin GÜL - avatar
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...:-)
19th Sep 2016, 11:11 AM
Descifrador
Descifrador - avatar