Thread: p++ and ++p

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    p++ and ++p

    hey again everyone, what is the difference in c between ++p and p++?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In what context? On their own, there isn't any difference that can be measured/noted [in C, in C++ if p is an object, ++p may be faster - but not always].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    As statements, as matsp noted, pretty much nothing. As expression, the difference is that:

    A = p++ is equal to A = p; p++;
    and
    A = ++p is equal to p++; A = p;

  4. #4
    Registered User Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    Code:
    int a,b;
    a=10;
    b=10;
    
    printf("%d\n",a++);
    printf("%d\n",++b);
    OUTPUT:
    10
    11

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    A more detailed explanation is that with ++p, the value of p is incremented BEFORE the expression it is part of. with p++, the value of p is incremented AFTER the expression it is part of. With both of them, the value of p is the same both before and after the expression has been evaluated.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    A nice article about ++p in for() loops in the FAQ: Cprogramming.com - C/C++ Programming Tips and Tricks
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    There's no explanation about WHY that makes a difference in a for loop.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    EDIT: Just realized this is the C board. Bladactania, there should be no difference in performance in C whether you pre- or post-increment in a for loop. I tend to pre-increment for consistency with my C++ coding, but there's no reason to prefer one over the other.

    Quote Originally Posted by Bladactania View Post
    There's no explanation about WHY that makes a difference in a for loop.
    What "x = p++" does:

    Code:
    Make a copy of p
    Increment p
    Return the copy by value
    That involves two copy constructor calls (one to make the copy, one to return it by value), one assignment operator call (to copy the return value into the result) one destructor call (to destroy the copy during return) and an increment.

    What "x = ++p" does:

    Code:
    Increment p
    Return p by reference
    That involves an increment and an assignment. Period.

    Obviously, these two things have different behavior. But in the context of a for-loop where the only purpose is to increment, obviously the "++p" method is more efficient.
    Last edited by brewbuck; 04-23-2009 at 12:37 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I don't understand the need for making a copy of "p" Couldn't it be returned by value then incremented?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    How do you increment after you have returned from the function?

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by brewbuck View Post
    What "x = p++" does:

    Code:
    Make a copy of p
    Increment p
    Return the copy by value
    That involves two copy constructor calls (one to make the copy, one to return it by value), one assignment operator call (to copy the return value into the result) one destructor call (to destroy the copy during return) and an increment.

    What "x = ++p" does:

    Code:
    Increment p
    Return p by reference
    I like that! Easy to understand
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed