Click to See Complete Forum and Search --> : Prefix and Postfix ++ and - -


August 10th, 1999, 07:27 PM
I just have some questions about the prefix and postfix versions of the ++ and -- statement. Correct me if I'm wrong but when the postfix ++ or -- statement it used it increments (or decrements) the variable it's connected to but returns the value of the variable directly before the ++ or -- statement was called so then
int i=5;
a = i++
would make a = 5 and i = 6;
When the prefix versions are used it increments or decrements depending on which is called the connected variable. But this time it returns the new value so if the prefix versioin was used above a would = 6. Now here's my question I have the following code in a program
int i=5;
c = (i++) + (i++);
and it makes c = 10, not 11. Why is that?

Wayne Fuller
August 10th, 1999, 07:46 PM
It seems like it would be doesn't it. But if you examine the assembly code this is what the following code would do.

int i = 5;

c = (i++) + (i++);

1) i is stored in register.
2) the register is added to the same register and stored in c.
3) i is incremented.

Makes sense huh,

Wayne