can anyone explain this output? int x=3; x=++x + x++; cout<<x; //output=9 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

can anyone explain this output? int x=3; x=++x + x++; cout<<x; //output=9

13th Jun 2016, 1:14 PM
Lekhraj Singh
Lekhraj Singh - avatar
4 Answers
+ 4
In that statement first x stores the value 3 then run next statement (x=++x + x++).in the statement x assign the value 8 (4+4) after that it increment once again due to post increment of x.so finally it gets 9 as the value in x.
13th Jun 2016, 1:58 PM
Lekhraj Singh
Lekhraj Singh - avatar
- 2
first the ++x makes ur x equals 4 then the x++ makes x= 5 (first 'x' stores the value 3 in the beginning and then 'x' becomes 4 and then the value again increases by 1 then it becomes 5) So the sum 4+5 becomes 9. I think it might help you out.
13th Jun 2016, 1:38 PM
MKS monkey
MKS monkey - avatar
- 2
1. 3=(1+3)+(3+1) 2. 4=(4)+(4+1) 3. 9=4+5
13th Jun 2016, 5:00 PM
Alex Dodge
Alex Dodge - avatar
- 5
in above expression solve right to left so first x++ value x is 4 then ++x value is 5 and add them 5+4=9. there is concept of sequence point
13th Jun 2016, 1:41 PM
Darshan Kavathekar
Darshan Kavathekar - avatar