+ 1
why
why x - x++ == 0 ? and not 1 ? what happens with "++" ?
8 Answers
+ 3
Oh, no, it's not like that, because ++ has higher precedence, it is evaluated first
so
instead of
(x - x) ++
it is
(x) - (x++)
see?
+ 2
Because x++ is post increment
So the increment happens after the value is taken out for subtraction
+ 1
example
if you have
a=1
b=a++
so
a=2
b=1
here
x=1
1-1++ ==0
and later x = 2
+ 1
Yes and it is because it is x++
If it is x - ++x, the result will be -1
0
I would imagine like:
x - x = 0 and then 0++ which is 1
0
@Gordon
I understand what ++x and x++ mean and in any other expression I am able to figure it out.
Also, I tried x = x - ++x, which outputs -1 and just by the same logic (to me) x = x - x++ should be 1, not 0.
0
@Gordon
Ok, now I see, thank you very much!
0
It not work because you are removing the x++ from x --> x - x++ you need that x++ or x = x++ to add one not remove one