The following two lines of code are not returning the same value. Any reason for that?
int i;
i = 1;
i = i + i++; //Returns 2, expecting 3
And
i = 1;
i = i++ + i; //Returns 3
Semantically, this should be the same a + b = b + a
right?
The same with decreasing i
:
i = 1;
i = i - i--; //Returns 0, expecting 1
And
i = 1;
i = i-- - i; //Returns 1, expecting -1
What confuses me even more is the usage of post increment operators:
i = 1;
i = i + ++i; //Returns 3
And
i = 1;
i = ++i + i; //Returns 4, expecting 3
Same again with decreasing operator:
i = 1;
i = i - --i; //Returns 1
And
i = 1;
i = --i - i; //Returns 0, expecting -1
Last Question:
How are these two lines interpreted by the compiler?
i = i+++i; // is it i + ++i or i++ + i?
i = i---i; // is it i - --i or i-- - i?