In C, what is the difference between using ++i
and i++
, and which should be used in the incrementation block of a for
loop?
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.
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.
++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.
You can think of internal conversion of that as a multiple statements;
i++;
you can think it as,
i;
i = i+1;
++i;
you can think it as,
i = i+i;
i;
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.
Lets see the following example:
int i = 1, j;
j = i++;
Here value of j = 1
but i = 2
. Here value of i
will be assigned to j
first 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++
.
Lets see the following example:
int i = 1, j;
j = ++i;
Here value of j = 2
but i = 2
. Here 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.. doesn't matter. It will execute your for loop same no. 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 same output. ie 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
.
Here is the example to understand the difference
int i=10;
printf("%d %d",i++,++i);
output: 10 12/11 11
(depending on the order of evaluation of arguments to the printf
function, which varies across compilers and architectures)
Explanation:
i++
->i
is printed, and then increments. (Prints 10, but i
will become 11)
++i
->i
value increments and prints the value. (Prints 12, and the value of i
also 12)