- 1
what will be the output of: int x=1; x=x++ + x++; cout<<x;
13 Answers
+ 3
acc to me it should be 3 because it will do as follows 1+2. First it will take x=1 as it is post fix and then as 2. So total will be 3.
Can someone just confirm ??
+ 3
Yeah!!! Programmer is right!... writing x++ + x ++ its like (((x)++) + x) which gives 1 + 2 that is equal to 3 and then again x++ that leave x equal to 3 but this last result is overwriten with the result from 1 + 2 that in this case is also 3.
If you have x++ + x ++ + x++ you will have 1 + 2 + 3 and with x = 4 after the third post-increment, but that value is overwriten by the result from the operatiob that is equal to 6... so at the end x equals 6...
I haven't notice that behaviour before... it's all about operator precedence... thanks programmer for open my eyes... ;-)
+ 2
3
+ 2
++x is pahle increase karo & phir use
x++ is use karo & then value increase karo aage k liye.
0
deepak
its (x++) + (x++)
Got it!!
0
yup it returns 3..? but how?
0
how can it be 4 ??
0
x=(1+1)+(1+1)
x=4
right?
- 1
tanx
- 2
I think it is 2
- 2
x+++x++; ??
- 2
I guess it should be 4.... but just for curiosity y run it in the Code Playground and got 3 as an output... so, I was driven to think that if you have several post-increment in the same statement for the same variable, only one is considered... but then I did another test with x = x++ + x++ + x++; and got 6 as the output....!!!????
So, I guess the code ground compiler it's not close to be perfect... ;-)
- 3
x+++ can be equivalent to x+2