A Brief Explanation
Let’s say we have int i = 0;, and imagine there’s a temporary object
(rvalue?) to store the result of i + 1 for the ++ operator.
Object is an area of memory that is used by our program, and temporary object in here means that the object has a temporary duration and will be deleted when the containing full expression ends.
Full expression here means:
- The complete expression that forms an expression statement (with terminating semicolon (;) at the end).
- One of the controlling expression
if,switch,while,for, ordo-whilestatement.- The expression of an initializer (like
int i = 0;).returnstatement.
This is what probably happen if we use i++ without assigned the result to another variable:
// i++;
tmp = i;
i = i + 1;
This is what probably happen if we use ++i without assigned the result to another variable:
// ++i;
i = i + 1;
tmp = i;
This is what probably happen if we use i++ when assigned the result to variable j:
// int j = i++;
tmp = i;
j = tmp;
i = i + 1;
This is what probably happen if we use ++i when assigned the result to variable j:
// int j = ++i;
i = i + 1;
tmp = i;
j = tmp;
The only difference between i++ and ++i is when we use the value of the
operation in the same statement, like int j = ++i.
P.S:
At the time of writing this post, i am still learning about C,
so this illustration might be wrong. Please let
me know if there are a better illustration about this.
i++ or ++i in For Loop
When using either i++ or ++i in for-loop like this:
int i;
for (i = 0; i < 5; i++)
printf("%d\n", i);
for (i = 0; i < 5; i++)
printf("%d\n", i);
Both of them will operate identically because the increment of i and the print
statement is in different line. It’s like we are using i++ or ++i
without any assignment like this:
int i = 0;
i++;
++i;
printf("%d\n", i);
Side Note
Please keep in mind that operation i++ and ++i is also prone to integer
overflow.
Here an overflow example:
#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned int i = UINT_MAX;
printf("before increment i: %d\n", i);
i++;
printf("after increment i: %d\n", i);
return 0;
}
In the example above, as we store the maximum value of unsigned integer in i
and when we increase the value of i, the result would be 0 because the value
is more than the maximum value of unsigned integer, so it result in overflow.