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