tags:

views:

7918

answers:

11

In C, what is the difference between using ++i and i++. And which should be used in the incrementation block of a for loop?

+1  A: 

++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.

Ryan Fox
I am not aware of any compiler where it does make a difference for integers at least.
blabla999
+45  A: 
  • ++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.

Mark Harrison
A: 

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
Both have undefined behavior.
R..
A: 

@Abhinav,

Since addition is associative, the answer to both is the same.

Brad Wilson
No, after undefined behavior, nothing is guaranteed, not even the associativity of addition.
R..
+4  A: 

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);
Drew Gibson
A: 

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.

On Freund
+3  A: 

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.

OysterD
-1 for C++ answer to C question. There is no more "local copy" of the value of `i` than there is of the value 1 when you write a statement `1;`.
R..
A: 

@Drew, whoops you're right, I typed it in exactly backwards. Thanks!

Mark Harrison
A: 

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

botismarius
This pathological example gets it wrong, sure, but there are plenty of legitimate ways to use pre- or post-increment within the argument list of a function call. This is fine, for example: int i = 100, j = 200; printf("%i;%i", i++, ++j); // "100;201" - guaranteed
John Zwinck
+2  A: 

@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.

See The Comp.Lang.C FAQ Article on Sequence Points

Frank Szczerba
A: 

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.

Andy Lester