Why the output is 13?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Why the output is 13??

int x=3; int y=(++x) +(x++)+(--x);

28th Jul 2019, 4:09 PM
GameHuB
GameHuB - avatar
11 Answers
+ 7
x was 3 first then [(++x)=5 (x++)=4] //first section [(--x)=4] //second section Then output is 13
29th Jul 2019, 5:26 AM
Aung Thiha
Aung Thiha - avatar
+ 6
(++x)=(++3)= it is prefix which performs evaluation and assign value so now x=4 (X++)=(4++)= it is postfix it assigns value and performs evaluation now x=5 (--X)=(--5)= again it is prefix so it performs decrement operation and assigns value so now x=4 So now 4 + 5 + 4 =13
30th Jul 2019, 12:30 PM
Ayushi Gujarati
Ayushi Gujarati - avatar
+ 2
why so??
28th Jul 2019, 5:24 PM
GameHuB
GameHuB - avatar
+ 1
The output of that line is compiler dependent. The behaviour of a few ++ and -- in one line is undefined.
28th Jul 2019, 4:55 PM
daniel
daniel - avatar
+ 1
Here answer is 12 as we go through the code , code internally storing value of x, first ++x it preincreaments x by 1 and stores value of x as 4 and later again post increment by 1 now x is 5 and finally x is pre decrement by 1 that is now again x becomes 4. Post increment works like a temporary increase in value for next operation, ++x is 4 and actual value of x++ is 4 but as we have next operation it increments its value by 1 and x becomes 5 and now predecreament by 1 that is --x is 4 but while carrying out whole operation it is as below, (++x)+(x++)+(--x) (3+1)+(4)+((x++)-1) 4 + 4 + (5 - 1) 4+4+4 = 12 Code working as below , for understanding splitting code after each operation. var x=3 { document.write(++x); //4 document.write(x); //4 } { var x=3 document.write((++x)+(x++)); //8 document.write(x); //5 } { var x=3 document.write((++x)+(x++)+(--x)); // 12 document.write(x); //4 }
28th Jul 2019, 9:16 PM
Anil
+ 1
😁
29th Jul 2019, 6:20 AM
Meghraj Shewale
+ 1
It depends on the compiler. In C there is something called sequence points. When the execution reaches one of these points, it assures you that the collateral effects will be over, while C can evaluate the operations in the order it deems appropriate. This is because the compiler looks for the most efficient way to execute the code.
18th Aug 2019, 10:57 AM
Miquel Andreu Fuster Sancho
Miquel Andreu Fuster Sancho - avatar
+ 1
The answer is; ++x=(1+3)=4 X++=(4+1)=5 --x=(5-1)=4 Final. y=4+5+4=13.
22nd Aug 2019, 5:19 AM
Rahul Gangwar
Rahul Gangwar - avatar
0
O/p shd be 12.. not 13!
31st Jul 2019, 5:17 AM
Satyam
Satyam - avatar
0
Here x=3 initially. ++3 which is a prefix notation assigns it as x=4. x++ which is a postfix notation uses x=4 but after the use it assigns x=5. Finally --x is also prefix notation thus it assigns x=4. Therefore result y=4+5+4=13.
6th Aug 2019, 1:34 PM
Harsh Sharma
Harsh Sharma - avatar
- 5
The answer is 4. ++x = 4 x++=4 (post increment) -x =-4 Answer = 4+4-4=4 IMHO 🤗🤗 🤗
30th Jul 2019, 3:35 AM
Sanjay Kamath
Sanjay Kamath - avatar