+ 8
Why the output is 13??
int x=3; int y=(++x) +(x++)+(--x);
11 Answers
+ 7
x was 3 first then
[(++x)=5
(x++)=4] //first section
[(--x)=4] //second section
Then output is 13
+ 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
+ 2
why so??
+ 1
The output of that line is compiler dependent. The behaviour of a few ++ and -- in one line is undefined.
+ 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
}
+ 1
đ
+ 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.
+ 1
The answer is;
++x=(1+3)=4
X++=(4+1)=5
--x=(5-1)=4
Final. y=4+5+4=13.
0
O/p shd be 12.. not 13!
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.
- 5
The answer is 4.
++x = 4
x++=4 (post increment)
-x =-4
Answer = 4+4-4=4 IMHO đ€đ€ đ€