+ 2

Please explain ( answered)

#include<iostream> using namespace std; int main() { int x = 10; int &y = x; x++; cout<< x << " " << y++; return 0; } The answer is x = 12 y = 11 Please can anyone explain? I got this question from this website: https://www.indiabix.com/cpp-programming/references/005001

10th Dec 2017, 2:33 PM
RR2001
RR2001 - avatar
3 Answers
+ 11
int x = 10; // value of x is 10. int &y =x; // x value is assigned here to y. x++ ; // here x becomes 11 and y too . Then cout rule --> that it executes from right to left. First y++ executes .its post increment so first y is printed then it is incremented. y = 11. Then cout print x . as y is post incremented now .and x takes that increment . and x will print 12.
10th Dec 2017, 2:49 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
@ GAWEN STEASY Are you sure it executes from right to left. because according to your answer you are trying to convey that the output will be. 11 12. but it is printed as 12 11
10th Dec 2017, 3:23 PM
RR2001
RR2001 - avatar