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;

4th Dec 2021, 7:39 AM
Nariman Tajari
Nariman Tajari - avatar
3 ответов
+ 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?
4th Dec 2021, 7:55 AM
Ipang
+ 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?
4th Dec 2021, 7:59 AM
Nariman Tajari
Nariman Tajari - avatar
+ 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.
4th Dec 2021, 8:05 AM
Ipang