++x is pre-increment and x++ is post-increment that is in the first x is incremented before being used and in the second x is incremented after being used.
This is most easily demonstrated with a small program.
- #include <stdlib.h>
- #include <stdio.h>
- int main(int argc, char **argp)
- int x = 5;
- printf(“x=%d\n”, ++x);
- printf(“x=%d\n”, x++);
- printf(“x=%d\n”, x);
- return EXIT_SUCCESS;
- The output of this program is
In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.