operator ++

This is a discussion on operator ++ within the C Programming forums, part of the General Programming Boards category; int i=0; printf("%d %d %d %d",++i,++i,++i,++i); output: 4 3 2 1 why is not 1 2 3 4...

  1. #1
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41

    Unhappy operator ++

    int i=0;

    printf("%d %d %d %d",++i,++i,++i,++i);

    output:
    4 3 2 1

    why is not
    1 2 3 4

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    because C doesn't guarantee that the first parameter of a function is evaluated before the second, and I think the behavior is undefined.
    try something like:
    Code:
    printf("%d %d %d %d",i+1,i+2,i+3,i+4);
    i += 4;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by glUser3f
    C doesn't guarantee that the first parameter of a function is evaluated before the second, and I think the behavior is undefined.
    You are correct. This is undefined behaviour. This means that it can be implemented in any way the compiler creator feels like because there is no standard definition of what is supposed to happen.

    Thus, doing it this way makes your code unportable, and a pain to debug when something doesn't quite work the way you think it should. It may work the way you intend on one compiler/setup, but slightly or hugely different on another.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Not only is the order of evaluation of parameters undefined, multiple side effects are also undefined as well (basically ++ applied to the same object multiple times in the same expression).
    http://www.eskimo.com/~scs/C-faq/s3.html

    So you could end up with all the ++ happening first, and the output would be
    4 4 4 4
    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.

  5. #5
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    I tried the code in both in lcc32 and gcc both are giving same output

    but for below code
    printf("%d %d",p(),k());

    function p() is called first in both lcc and gcc.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > I tried the code in both in lcc32 and gcc both are giving same output
    Ah, the "works for me" defence your honor.
    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
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by shiju
    I tried the code in both in lcc32 and gcc both are giving same output

    but for below code
    printf("%d %d",p(),k());

    function p() is called first in both lcc and gcc.
    So try it in Borland C, Mark Williams C, Lattice C, Visual C, Watcom C, Tiny C, etc, etc....

    If you find one that doesn't give the same output, therein lies the portability problem.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    I feel the ++ operator has higher precedence then ,(comma) operator.And ++ operator is evaluated from right to left.

    thats why the statement

    printf("%d %d %d %d",++i,++i,++i,++i);

    is evaluated as

    printf("%d %d %d %d",4,3,2,1);

    please correct me if I am wrong

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by rakesh
    It's already been stated: It is undefined. It is up to the compiler to interpret this as it sees fit in regards to the specific way it's being used.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    printf("%d %d %d %d",++i,++i,++i,++i);

    Gives 4 4 4 4 on MSVC.

    An excellent article is available on this subject (it's more complicated than left to right or right to left):
    http://www.embedded.com/story/OEG20020429S0037

    >>I feel the ++ operator has higher precedence then ,(comma) operator.<<

    You are confusing the 'comma operator' and the 'comma argument seperator'. There is no comma operator in the above statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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