Thread: Increment and Decrement

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Belfast, Northern Ireland
    Posts
    9

    Increment and Decrement

    Hi, just started out with C a day or so ago using the book from Apress publishing "Learn C on the Mac" it's a really good book and they explain it well but there's one thing I don't understand at all. The increment and decrement using ++ and --, i get 5++ would give you 6 and 5-- would give you 4. But it also tries to explain how and when to use ++5 or --5 but I din't understand that at all.
    Could someone maybe briefly explain it please?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    ++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 sample program might be of use here

    [insert]
    Code:
        #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 would be

    6
    6
    7

    So you see in the first case x is incremented before being printed to the console
    In the second case the value of x is incremented after the value is printed to the console
    and in the third the result of post increment in the previous printf can be seen.

    Hope this makes it a bit clearer.
    Last edited by roaan; 07-25-2009 at 01:56 PM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Belfast, Northern Ireland
    Posts
    9
    That's a bit easier to understand thanks. But when would you need to use pre over post?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Johnathan1707 View Post
    That's a bit easier to understand thanks. But when would you need to use pre over post?
    When order matters. (That is, when you want to use the value of "one more than the variable" instead of using the value of the variable itself.)

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Belfast, Northern Ireland
    Posts
    9
    Oh, I think I get it now. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  2. newb need help with increment and decrement
    By cgurl05 in forum C Programming
    Replies: 2
    Last Post: 03-05-2006, 04:50 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM