Assuming standard headers and using namespace std, what does this C++ code print? int x = 5; int y = x++ + 2; cout << x << ” ” << y;
Correct answer A. 6 7
Explanation
The postfix x++ yields the old value of x (5) and then increments x to 6. So y receives 5 + 2 = 7 while x ends at 6. A prefix ++x would have produced 8 for y. Here x is modified only once in the full expression, so the behaviour is well defined.
Answer checked How we write and check questions
Discussion