In C, what is the difference between using ++i
and i++
, and which should be used in the incrementation block of a for
loop?
Shortly:
++i
and i++
works same if you are not writing them in a function. If you use something like function(i++)
or function(++i)
you can see the difference.
function(++i)
says first increment i by 1, after that put this i
into the function with new value.
function(i++)
says put first i
into the function after that increment i
by 1.
int i=4;
printf("%d\n",pow(++i,2));//it prints 25 and i is 5 now
i=4;
printf("%d",pow(i++,2));//it prints 16 i is 5 now