In C, what is the difference between using ++i
and i++
, and which should be used in the incrementation block of a for
loop?
They both increment the number. ++i
is equivalent to i = i + 1
.
i++
and ++i
are very similar but not exactly the same. Both increment the number, but ++i
increments the number before the current expression is evaluated, whereas i++
increments the number after the expression is evaluated.
Example:
int i = 1;
int x = i++; //x is 1, i is 2
int y = ++i; //y is 3, i is 3