Question:
question on increments?
anonymous
2014-07-03 08:30:00 UTC
I can't seem to grasp the concept behind this. They say x++ would be incrementing the value later while ++x is incrementing beforehand. But despite this, the tests I run seem to give the opposite of what's stated. Isn't x++ and ++x supposed to be the same thing? But when I run this code for example:
int i;
i = 2;
i = ++i;
and
int i;
i = 2;
i = I++;
I get two different answers instead: 3 and 2 respectively.

And for
int i = 5;
int x = 5;
x = i = i++; and x = i = ++i
I get the same answers differently for both instances: i & x both equals 5 and i & x both equals 6 respectively.

What's the explanation for this?
Three answers:
Kevin
2014-07-03 08:47:31 UTC
Okay, the difference between prefix (++x) and postfix (x++) is rather simple. Both have the concept of incrementing the variable - for numbers, this means add one, for pointers, it means move to the next thing to point at.



Prefix is simple. It increments the value, and then returns the new value. Imaging that you have an int variable, x, and it holds the value 6. ++x would set x's value to 7, and return 7.



Postfix, however, is different. It's not about the timing of the incrementing (after all, the incrementing has to happen in the body of the function, before the return statement, in both pre- and postfix operators). Instead, it's easier to think about it as the postfix operator makes a copy of the variable's original state. Then, like the prefix operator, it would increment the variable in place. However, unlike prefix, which just returns the variable, post-fix returns the (unmodified) copy.



Thus, if you had an int variable, y, which had the value 12, y++ would set y's value to 13, but return 12.



Also, your assignment statements are pretty useless. Note that the return value and the variable's value will be the same for prefix, so something like (x = ++x) will never be useful. Likewise, (x = x++) is also equally useless, though for less obvious reasons - postfix returns the unmodified value. As in, you wouldn't change the value of the variable at all!



Basically, (x = ++x;) is equivalent to simply (++x;) and (x = x++) does nothing at all.



EDIT: I'm surprised I didn't bring this up, but my discussion on your use of assignment statements is the intended semantics. It is guaranteed for built-ins and for classes in the standard that define those operators, however it is not guaranteed in all cases.



In C++, programmers could make the pre and postfix operators do whatever they want. And, even if they do the incrementing, one could imagine a class that keeps a count of how many instances have been constructed (without reducing the count when an instance goes out of scope). Thus, the postfix, which explicitly makes a copy, would increment that count more than prefix.
Michael Chang
2014-07-03 22:21:00 UTC
@Kevin: I think your answer, while definitely correct, is too complicated for such a simple question. You don't want to confuse the poor guy/gal :p Then again, I'm not sure how *I* would give an explanation. I understand and use pre- and post-fix operators, but I've never had to explain them.



Example 1:

int i = 1;



return(i++); // returns 1 because you're returning and THEN incrementing*



Example 2:

int i = 1;



return(++i); // returns 2 because you're incrementing and THEN returning



I guess I'd explain it by simply saying that it's about the order of evaluation.



[*]: Well, okay, in reality you're not actually incrementing after the return, since the variable has gone out of scope by then, but you get the idea.
justme
2014-07-04 05:36:51 UTC
Think arrays...

int array[3];

int number = 0;



array[0] = 10;

aray[1] = 100;

array[2] = 1000;

...

number = array[number]; <----- after execution number contains 10

number = array[++number]; <---- after execution number contains 100 because number is incremented before it is used

number = array[number++]; <---- after execution number contains 100 because number is incremented after it is used

number = array[number]; <--- after execution number contains 1000


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...