In C, what is the difference between using ++i and i++. And which should be used in the incrementation block of a for loop?
++i increments the value, then returns it.
i++ returns the value, and then increments it.
It's a subtle difference.
For a for loop, use ++i, as it's slightly faster. i++ will create an extra copy that just gets thrown away.
++i will increment the value of i, and then return the incremented value.
i = 1; j = ++i; (i is 2, j is 2)
i++ will increment the value of i, but return the pre-incremented value.
i = 1; j = i++; (i is 2, j is 1)
For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.
In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.
update: there's a couple of comments regarding the efficiency of ++i and i++. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.
update 2: the efficiency question is interesting... here's my attempt at an answer:
http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i
update 3: As On Freund notes, it's different for a C++ object, since operator++() is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.
Gotcha : Using both in the same statement can be unintuitive. I remember this being a good trick question earlier.
x = i++ + ++i ; v/s x = ++1 + i++
What is the value of x in each of the two statements ? :)
@Abhinav,
Since addition is associative, the answer to both is the same.
Mark, can you edit your answer ? I don't have enough rep yet, so I can't.
You talk about ++i, then give an example of i++, and it seems to me that your second example is incorrect.
Seems to me this should read...
++i will increment the value of i, and then return the incremented value.
i = 1; j = ++i; (j is 2, i is 2)
i++ will increment the value of i, but return the pre-incremented value.
i = 1; k = i++; (k is 1, i is 2)
edit by Mark: Thanks Drew, now fixed!
edit, @The.Anti.9...
these two loops will produce identical results...
printf("\nfor(i = 1; i <= 10, i++)\n");
for(i = 1; i <= 10; i++)
printf("%3d\n", i);
printf("\nfor(i = 1; i <= 10, ++i)\n");
for(i = 1; i <= 10; ++i)
printf("%3d\n", i);
Not sure the original poster is interested, but in C++ the difference in performance can be substantial, since the creation of the temporary object might be expensive for a user defined type.
The reason ++i can be slightly faster than i++ is that i++ can require a local copy of the value of i before it gets incremented, while ++i never does. In some cases, some compilers will optimize it away if possible... but it's not always possible, and not all compilers do this.
I try not to rely too much on compilers optimizations, so I'd follow Ryan Fox's advice: when I can use both, I use ++i.
@Drew, whoops you're right, I typed it in exactly backwards. Thanks!
Avoid using ++i and/or i++ in a function call. The result of the next instruction is not determined according to C standard:
int i=100; printf("%i;%i",i++,i);
Some compilers will display: 100;100
while others will display: 100;101
@Ahinav and Brad Wilson:
The statements
x = i++ + ++i;
and
x = ++i + i++;
are undefined in C (meaning the compiler can do whatever it wants with them). The standard says:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
Please don't worry about the "efficiency" (speed, really) of which one is faster. We have compilers these days that take care of these things. Use whichever one makes sense to use.