pre and post increment C and C++
i++ shows that the value of i is post incremented after operation, ++i shows that its pre incremented before operation.
Example
#include<stdio.h> int main() { int i = 5; //-------------------------------- //--------POST INCREMENT---------- printf("5 because of post increment : %d\n", i++); printf("6 because value incremented above : %d\n",i); // execution from left to right, this will output 6 7 8 printf("%d %d %d\n",i++ ,i++ ,i++); // output 9 printf("output 9 : %d\n",i); //-------------------------------- //--------PRE INCREMENT---------- printf("10 because of pre increment : %d\n",++i); printf("10 because same : %d\n",i); // execution from left to right, this will output 10 11 12 printf("%d %d %d\n", ++i,++i,++i); // output 9 printf("output 13 : %d\n",i); getchar(); return 0; }
Output
5 because of post increment : 5 6 because value incremented above : 6 8 7 6 output 9 : 9 10 because of pre increment : 10 10 because same : 10 13 12 11 output 13 : 13