Which increment statement is faster among: ++i, i++ and i=i+1?
It depends on how your compiler generates the code for these statements. Most of the modern compilers are smart enough to optimize and generate the same instructions for all the three statements, so there wouldn't be any difference with respect to its speed.
For instance, take the example below:
#include<stdio.h>
int main
(void)
{
int i=
0;
i++;
++i;
i=i
+1;
printf("%d\n",i
);
return 0;
}
The above program when compiled with GCC, will generates the same instructions for all the three statements. But we cannot say all compilers will generate the same code for those statements, as there is nothing said by the standard regarding that.
However, you will see difference when you use the value returned by the expression as in the below program:
#include<stdio.h>
int main
(void)
{
int i=
0, j;
j = i++;
printf("%d\n",j
);
j= ++i;
printf("%d\n",j
);
return 0;
}
In this case, the compiler would not optimize, and would generate different statements for pre-increment and post-increment statements, but what exactly the compiler generates will still depend on which compiler you're using.