- 1

what will be the output of: int x=1; x=x++ + x++; cout<<x;

14th Jul 2016, 4:47 PM
Programmer
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 ??
14th Jul 2016, 5:16 PM
Programmer
+ 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... ;-)
14th Jul 2016, 7:23 PM
Nelson Urbina
Nelson Urbina - avatar
+ 2
3
15th Jul 2016, 10:08 AM
SIDDHARTH VASU
SIDDHARTH VASU - avatar
+ 2
++x is pahle increase karo & phir use x++ is use karo & then value increase karo aage k liye.
15th Jul 2016, 2:55 PM
SIDDHARTH VASU
SIDDHARTH VASU - avatar
0
deepak its (x++) + (x++) Got it!!
14th Jul 2016, 5:06 PM
Programmer
0
yup it returns 3..? but how?
14th Jul 2016, 5:08 PM
deepak h.s
deepak h.s - avatar
0
how can it be 4 ??
14th Jul 2016, 5:13 PM
Programmer
0
x=(1+1)+(1+1) x=4 right?
14th Jul 2016, 5:22 PM
deepak h.s
deepak h.s - avatar
- 1
tanx
15th Jul 2016, 11:35 AM
Programmer
- 2
I think it is 2
14th Jul 2016, 5:03 PM
sarath chandran r
sarath chandran r - avatar
- 2
x+++x++; ??
14th Jul 2016, 5:04 PM
deepak h.s
deepak h.s - avatar
- 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... ;-)
14th Jul 2016, 5:06 PM
Nelson Urbina
Nelson Urbina - avatar
- 3
x+++ can be equivalent to x+2
14th Jul 2016, 5:04 PM
deepak h.s
deepak h.s - avatar