In C, what is the difference between using ++i
and i++
, and which should be used in the incrementation block of a for
loop?
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
.