difference between ++ and +=1

This is a discussion on difference between ++ and +=1 within the C Programming forums, part of the General Programming Boards category; Hi all, Can any one tell the difference between the following. a++ a+=1 Both will do the same operation.I want ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    difference between ++ and +=1

    Hi all,
    Can any one tell the difference between the following.

    a++
    a+=1

    Both will do the same operation.I want to know that any one of these have a advantage than the other.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    While ++a and a++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the RHS of an assignment statement.
    For eg.
    a=5;
    y=++a;
    In this case the value of y would be 6. But in this case
    a=5;
    y=a++;
    here y would be 5 but a would be 6
    On the other hand a+=1 is shorthand form of a=a+1.
    Last edited by BEN10; 07-07-2009 at 06:47 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Thank you BEN10.But already I know what you explained.I want to know whether both of these statement have some difference in the case of speed or not.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    They are two different kinds of operators. ++ is a unary operator (whether x++ or ++x). += is an assignment operator. I doubt there is much difference in speed but I don't know. The potential use value is slightly different:
    Code:
    x += y++;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I'd say that it depends on compiler optimisations.

    I'm guessing that ++x would be translated by most compilers into the assembly 'INC' (increment) instruction mnemonic, which may or may not be faster than 'ADD'.

    If a compiler optimises any code with a 'x + 1' to the increment instruction then there would be no difference.

    Same goes for decrementing.

    Note that this is just speculation. I don't know if any of this is what actually happens.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Assignment operators evaluate the l-value only once, which is an important thing to watch out for if it contains any side effects.

    Eg.
    array[i++] += 1;
    will do the right thing.

    Either of these attempts leads to all sorts of trouble.
    array[i++] = array[i] + 1;
    array[i++] = array[i++] + 1;


    Doing it correctly the long way is hard work.
    array[i] = array[i] + 1;
    i = i + 1;


    There would certainly be a big performance difference if you had
    array[someFuncReturningIndex()] += 1;
    because the function would only be called once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    @Salem: good use of fonts
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Just an FYI,
    I read about the origin of "C", and how the compiler favored the reigning hardware platform of that time - PDP family of minis. So the post-increments and similar had a reason for being part of the language. There were native machine instructions of that ilk: access-then-increment. I haven't encountered any micro processors which had similar instructions but then again, the desired effect can be achieved with two instructions at most.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Just an FYI,
    I read about the origin of "C", and how the compiler favored the reigning hardware platform of that time - PDP family of minis. So the post-increments and similar had a reason for being part of the language. There were native machine instructions of that ilk: access-then-increment. I haven't encountered any micro processors which had similar instructions but then again, the desired effect can be achieved with two instructions at most.
    Both VAX (32-bit version of PDP-11 really) and 68000 have post-increment and pre-decrement versions of it's instruction set. I'm fairly sure that ARM's instructions also matches the C language fairly well with it's STR/LDR instructions when setting the P and W bits to one of the instruction.

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

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Thank you! I stand corrected. I forgot about Motorolas. Never could wrap my head around 6800, 68000 addressing, registers, but probably the assembly syntax turned me off most of all. I'm a Zilog/Intel guy myself.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Quote Originally Posted by DeadPlanet View Post
    I'd say that it depends on compiler optimisations.

    I'm guessing that ++x would be translated by most compilers into the assembly 'INC' (increment) instruction mnemonic, which may or may not be faster than 'ADD'.

    If a compiler optimises any code with a 'x + 1' to the increment instruction then there would be no difference.

    Same goes for decrementing.

    Note that this is just speculation. I don't know if any of this is what actually happens.
    x++ doesn't mean that the increment value is 1, it can be anything, so the use of something like "INC" is a very special case.

    Stephane.
    Last edited by Salem; 07-08-2009 at 09:54 AM. Reason: snip fake sig

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    x++ doesn't mean that the increment value is 1, it can be anything
    It doesn't? How do you set it to increment by something else?

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> x++ doesn't mean that the increment value is 1, it can be anything, so the use of something like "INC" is a very special case.

    Well, the only other case is in pointer arithmetic, where the increment means "advance sizeof(type) bytes from this offset". Besides that, though, I can't think of any other possible forms.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Back to the original question... even in the case of pointer arithmetic a++ and a+=1 would still be equivalent. If a compiler generates markedly different machine code which varies in execution times, then shame on it!

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Yes Sebastiani, I was referring to pointer arithmetic which I think it is important to mention, since the ++ operator comes from the PDP 11 autoincrement mode : (Rn)+ (see Department of Computer Science - PDP-11 - Don S. Bidulock).

    Often the question asked is "which is faster, x++ or x += 1 ?". In that case it's important to remember that "1" can be any other value (since an incrementer != adder).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21