differ b/w pre increment and post increment

++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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char **argp)
  5. {
  6.     int x = 5;
  7.  
  8.     printf(“x=%d\n”, ++x);
  9.     printf(“x=%d\n”, x++);
  10.     printf(“x=%d\n”, x);
  11.  
  12.     return EXIT_SUCCESS;
  13. }
  14. The output of this program is
    Expand|Select|Wrap|Line Numbers
    1. x=6
    2. x=6
    3. x=7

    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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s