posted 21 years ago
This has to do with the way pre- and postincrements work. With i++ (post-increment), first i is evaluated, then it is incremented. With ++i(pre-increment), first i is incremented, then evaluated.
int a = i++ - ++i
This works as follows: Initially, i is 0. When trying to do the substraction, the compiler evaluates the first operand, then the second, then it does the substraction.
So, first operand is evaluated to 0, then i is incremented, so it becomes 1. Then, second operand is evaluated, so first i is incremented, it becomes 2, then follows the evaluation, resulting in the current value of i which is 2. So now we have first operand = 0, second operand = 2, substraction results in -2.