What is the difference between ++i and i++?

# i++ and ++i

This little code may help to visualize the difference from a different angle than the already posted answers:

``````int i = 10, j = 10;

printf ("i is %i \n", i);
printf ("i++ is %i \n", i++);
printf ("i is %i \n\n", i);

printf ("j is %i \n", j);
printf ("++j is %i \n", ++j);
printf ("j is %i \n", j);
``````

The outcome is:

``````//Remember that the values are i = 10, and j = 10

i is 10
i++ is 10     //Assigns (print out), then increments
i is 11

j is 10
++j is 11    //Increments, then assigns (print out)
j is 11
``````

Pay attention to the before and after situations.

# for loop

As for which one of them should be used in an incrementation block of a for loop, I think that the best we can do to make a decision is use a good example:

``````int i, j;

for (i = 0; i <= 3; i++)
printf (" > iteration #%i", i);

printf ("\n");

for (j = 0; j <= 3; ++j)
printf (" > iteration #%i", j);
``````

The outcome is:

``````> iteration #0 > iteration #1 > iteration #2 > iteration #3
> iteration #0 > iteration #1 > iteration #2 > iteration #3
``````

I don't know about you, but I don't see any difference in its usage, at least in a for loop.

The following C code fragment illustrates the difference between the pre and post increment and decrement operators:

``````int  i;
int  j;
``````

Increment operators:

``````i = 1;
j = ++i;    // i is now 2, j is also 2
j = i++;    // i is now 3, j is 2
``````

The difference can be understood by this simple C++ code below:

``````int i, j, k, l;
i = 1; //initialize int i with 1
j = i+1; //add 1 with i and set that as the value of j. i is still 1
k = i++; //k gets the current value of i, after that i is incremented. So here i is 2, but k is 1
l = ++i; // i is incremented first and then returned. So the value of i is 3 and so does l.
cout << i << ' ' << j << ' ' << k << ' '<< l << endl;
return 0;
``````

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

I assume you understand the difference in semantics now (though honestly I wonder why people ask 'what does operator X mean' questions on stack overflow rather than reading, you know, a book or web tutorial or something.

But anyway, as far as which one to use, ignore questions of performance, which are unlikely important even in C++. This is the principle you should use when deciding which to use:

Say what you mean in code.

If you don't need the value-before-increment in your statement, don't use that form of the operator. It's a minor issue, but unless you are working with a style guide that bans one version in favor of the other altogether (aka a bone-headed style guide), you should use the form that most exactly expresses what you are trying to do.

QED, use the pre-increment version:

``````for (int i = 0; i != X; ++i) ...
``````

`++i` (Prefix operation): Increments and then assigns the value
(eg): `int i = 5`, `int b = ++i` In this case, 6 is assigned to b first and then increments to 7 and so on.

`i++` (Postfix operation): Assigns and then increments the value
(eg): `int i = 5`, `int b = i++` In this case, 5 is assigned to b first and then increments to 6 and so on.

Incase of for loop: `i++` is mostly used because, normally we use the starting value of `i` before incrementing in for loop. But depending on your program logic it may vary.

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, based on which more clearly shows your intent.

`i++`: In this scenario first the value is assigned and then increment happens.

`++i`: In this scenario first the increment is done and then value is assigned

Below is the image visualization and also here is a nice practical video which demonstrates the same. i++ is known as post increment whereas ++i is called pre increment.

`i++`

`i++` is post increment because it increments `i`'s value by 1 after the operation is over.

Let’s see the following example:

``````int i = 1, j;
j = i++;
``````

Here value of `j = 1`, but `i = 2`. Here the value of `i` will be assigned to `j` first, and then `i` will be incremented.

`++i`

`++i` is pre increment because it increments `i`'s value by 1 before the operation. It means `j = i;` will execute after `i++`.

Let’s see the following example:

``````int i = 1, j;
j = ++i;
``````

Here the value of `j = 2` but `i = 2`. Here the value of `i` will be assigned to `j` after the `i` incremention of `i`. Similarly, `++i` will be executed before `j=i;`.

For your question which should be used in the incrementation block of a for loop? the answer is, you can use any one... It doesn't matter. It will execute your for loop same number of times.

``````for(i=0; i<5; i++)
printf("%d ", i);
``````

And

``````for(i=0; i<5; ++i)
printf("%d ", i);
``````

Both the loops will produce the same output. I.e., `0 1 2 3 4`.

It only matters where you are using it.

``````for(i = 0; i<5;)
printf("%d ", ++i);
``````

In this case output will be `1 2 3 4 5`.

The only difference is the order of operations between the increment of the variable and the value the operator returns.

This code and its output explains the the difference:

``````#include<stdio.h>

int main(int argc, char* argv[])
{
unsigned int i=0, a;
printf("i initial value: %d; ", i);
a = i++;
printf("value returned by i++: %d, i after: %d\n", a, i);
i=0;
printf("i initial value: %d; ", i);
a = ++i;
printf(" value returned by ++i: %d, i after: %d\n",a, i);
}
``````

The output is:

``````i initial value: 0; value returned by i++: 0, i after: 1
i initial value: 0;  value returned by ++i: 1, i after: 1
``````

So basically `++i` returns the value after it is incremented, while `i++` return the value before it is incremented. At the end, in both cases the `i` will have its value incremented.

Another example:

``````#include<stdio.h>

int main ()
int i=0;
int a = i++*2;
printf("i=0, i++*2=%d\n", a);
i=0;
a = ++i * 2;
printf("i=0, ++i*2=%d\n", a);
i=0;
a = (++i) * 2;
printf("i=0, (++i)*2=%d\n", a);
i=0;
a = (i++) * 2;
printf("i=0, (i++)*2=%d\n", a);
return 0;
}
``````

Output:

``````i=0, i++*2=0
i=0, ++i*2=2
i=0, (++i)*2=2
i=0, (i++)*2=0
``````

## Many times there is no difference

Differences are clear when the returned value is assigned to another variable or when the increment is performed in concatenation with other operations where operations precedence is applied (`i++*2` is different from `++i*2`, as well as `(i++)*2` and `(++i)*2`) in many cases they are interchangeable. A classical example is the for loop syntax:

``````for(int i=0; i<10; i++)
``````

has the same effect of

``````for(int i=0; i<10; ++i)
``````

## Efficiency

Pre-increment is always at least as efficient as post-increment: in fact post-increment usually involves keeping a copy of the previous value around and might add a little extra code.

As others have suggested, due to compiler optimisations many times they are equally efficient, probably a for loop lies within these cases.

## Rule to remember

To not make any confusion between the two operators I adopted this rule:

Associate the position of the operator `++` with respect to the variable `i` to the order of the `++` operation with respect to the assignment

Said in other words:

• `++` before `i` means incrementation must be carried out before assignment;
• `++` after `i` means incrementation must be carried out after assignment:

The Main Difference is

• i++ Post(After Increment) and
• ++i Pre (Before Increment)

• post if `i =1` the loop increments like `1,2,3,4,n`
• pre if `i =1` the loop increments like `2,3,4,5,n`

You can think of the internal conversion of that as multiple statements:

``````// case 1

i++;

/* you can think as,
* i;
* i= i+1;
*/

// case 2

++i;

/* you can think as,
* i = i+i;
* i;
*/
``````

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