0
What is the output of this code and why?! I think it should be 7613 but the compiler output is something else!
int x,y,z; x=5; y=++x; z=y++ + ++x; cout<<x<<y<<z;
3 Answers
+ 1
x = 5;
y = ++x; // <x> = 6, <y> = 6
z = y++ + ++x; // <y> = 6 <x> = 7, <z> = (6 + 7) => 13
cout << x << y << z; // <x> = 7, <y> = 7, <z> = 13
Does it help clarify?
+ 1
Ipang the problem is as yourself wrote y is 6 before std::cout , but in the end it counted as "7"! y why shouldnt be 6?
+ 1
Because the post-increment operation takes effect after the respective line has been processed. So the modification of <y> value is effective on the next line, where <x>, <y> and <z> are printed.