+ 2

How did this result come out is 8181?

int x=80; int &y=x; ++x; cout<<x<<y;

5th Aug 2020, 7:39 PM
Vladimir Kushner
Vladimir Kushner - avatar
11 Answers
+ 4
If You Want Explanation Then Here I Am Telling You Step By Step First You Initialised And Declared An Integer Type Variable With Value 80 Now After That You Initialised A Reference Variable y which is holding address of variable x and when you performed pre increment on x this will increase the value of x by 1 so when you printed x this gave you 81 and when you printed y who’s pointing to x first go to memory location of x then printed the value of x which is 81 SO ATLAST YOUR OUTPUT WILL BE 8181. This Is The Easiest Way I Can Explain This To You Vladimir
5th Aug 2020, 8:11 PM
Rahul Saxena
Rahul Saxena - avatar
+ 4
This post will help u to understood the concepts of reference variable https://www.geeksforgeeks.org/references-in-c/amp/
6th Aug 2020, 5:44 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Okay You Want Explanation About How This Code Gave You Output 8181 ?
5th Aug 2020, 8:05 PM
Rahul Saxena
Rahul Saxena - avatar
+ 1
Yes 8181.
5th Aug 2020, 8:06 PM
Armen Petrosyan
Armen Petrosyan - avatar
+ 1
Int &y=x means that if you increase x or y second also changed since both variables link the same memory. But if you write int y=x in this case you will create copy of x value. So bot variables in this case link to different memory
5th Aug 2020, 8:13 PM
george
george - avatar
+ 1
Absolutel right! First go to memory location x and then print the value of x. x=81 and y=81.
5th Aug 2020, 8:17 PM
Armen Petrosyan
Armen Petrosyan - avatar
+ 1
Thanks george I Just Mismatched With Terms
5th Aug 2020, 8:29 PM
Rahul Saxena
Rahul Saxena - avatar
+ 1
Vladimir budut voprosi po c++ pishite v lichku pomogu chem smogu
5th Aug 2020, 8:34 PM
george
george - avatar
0
Ryan Daniel not pointer but reference! There are a big difference between pointer and reference
5th Aug 2020, 8:25 PM
george
george - avatar
0
Ryan Daniel you are welcome! The great explanation!
5th Aug 2020, 8:32 PM
george
george - avatar