order of evaluation of increment operators

Discussion in 'C Programming' started by Rajorshi, Jul 22, 2004.

  1. Rajorshi

    Rajorshi Guest

    Hi,
    I am relatively new to c programming. I have a question regarding C's
    increment (++) operator. KnR says that unary operators like ++ associate
    from right to left. Having that information, I still dont understand in
    which order the following statements are evaluated :

    x+++y
    x++++y
    x+++++y and so on.
    I mean how does the compiler decide
    whether
    x+++y is x + (++y) or (x++) + y .

    I am obviously missing something...

    Thanks in advance!
     
    Rajorshi, Jul 22, 2004
    #1
    1. Advertising

  2. In article <>,
    (Rajorshi) wrote:

    > Hi,
    > I am relatively new to c programming. I have a question regarding C's
    > increment (++) operator. KnR says that unary operators like ++ associate
    > from right to left. Having that information, I still dont understand in
    > which order the following statements are evaluated :
    >
    > x+++y
    > x++++y
    > x+++++y and so on.
    > I mean how does the compiler decide
    > whether
    > x+++y is x + (++y) or (x++) + y .


    If you don't know what a statement does, then don't use it.

    There is no need to ever write code like "x+++y". If you find it used
    anywhere, instead of trying to figure out what it does find the
    programmer who wrote it, get a printed copy of the C++ Standard (not the
    C Standard, because the C++ Standard is larger and heavier) and hit that
    programmer with it until he or she changes the code.

    Apart from that, the compiler builds tokens from left to right, taking
    as many characters as possible. So if the compiler sees a '+' character,
    followed by another '+' character, the result is never two '+' tokens
    but always a single '++' token.
     
    Christian Bau, Jul 22, 2004
    #2
    1. Advertising

  3. Rajorshi

    sameer oak. Guest

    > Having that information, I still dont understand in
    > which order the following statements are evaluated :
    > x+++y
    > x++++y
    > x+++++y and so on.
    > I mean how does the compiler decide whether
    > x+++y is x + (++y) or (x++) + y .
    >
    > I am obviously missing something...

    ....
    the reason is operator precedence and the way they are associated with
    the
    operands.
    as you have mentioned in the above example, x+++y, the ++ operator has
    higher
    precedence over + operator.
    if you'd done something like this, x+(++y), the parenthesis were
    executed first.

    in the 2nd case, x++++y, the compiler tries to attach ++ operator to
    both the
    operands but cannot succeed to interpret the operation and thus issues
    an error
    for ``y'', something of this sort:
    ---
    foo.c: In function `main':
    foo.c:6: error: invalid lvalue in increment
    foo.c:6: error: parse error before "y"
    ---

    the last case is really confusing, x+++++y. am sure the error here is
    again "invalid lvalue." someone from the compilers can post a more
    appropriate answer for the last case.
    ..
     
    sameer oak., Jul 22, 2004
    #3
  4. In article <> (sameer oak.) writes:
    > the last case is really confusing, x+++++y. am sure the error here is
    > again "invalid lvalue." someone from the compilers can post a more
    > appropriate answer for the last case.


    The compiler parses it as: x ++ ++ + y.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Jul 22, 2004
    #4
  5. Rajorshi

    Rajorshi Guest

    Hi,
    Hey thanks a lot ! But before you flame me further - I would like to
    tell you
    that I have NO intention of writing such code. Its just that these
    type of
    questions are actually being asked at exams and interviews of large
    software companies (no, not FSF).

    > programmer who wrote it, get a printed copy of the C++ Standard (not the
    > C Standard, because the C++ Standard is larger and heavier) and hit that
    > programmer with it until he or she changes the code.
    >


    :)), good one

    So you mean that when the compiler sees something like
    x+++y, it considers it as x++ + y and then evaluates it.
    Hmm... makes sense... (silly me...)
    Thanks a lot, problem seems to have been solved (for now that is :)


    Christian Bau <> wrote in message news:<>...
    > In article <>,
    > (Rajorshi) wrote:
    >
    > > Hi,
    > > I am relatively new to c programming. I have a question regarding C's
    > > increment (++) operator. KnR says that unary operators like ++ associate
    > > from right to left. Having that information, I still dont understand in
    > > which order the following statements are evaluated :
    > >
    > > x+++y
    > > x++++y
    > > x+++++y and so on.
    > > I mean how does the compiler decide
    > > whether
    > > x+++y is x + (++y) or (x++) + y .

    >
    > If you don't know what a statement does, then don't use it.
    >
    > There is no need to ever write code like "x+++y". If you find it used
    > anywhere, instead of trying to figure out what it does find the
    > programmer who wrote it, get a printed copy of the C++ Standard (not the
    > C Standard, because the C++ Standard is larger and heavier) and hit that
    > programmer with it until he or she changes the code.
    >
    > Apart from that, the compiler builds tokens from left to right, taking
    > as many characters as possible. So if the compiler sees a '+' character,
    > followed by another '+' character, the result is never two '+' tokens
    > but always a single '++' token.
     
    Rajorshi, Jul 22, 2004
    #5
  6. Rajorshi

    Dan Pop Guest

    In <> (Rajorshi) writes:

    >Hi,
    > I am relatively new to c programming. I have a question regarding C's
    >increment (++) operator. KnR says that unary operators like ++ associate
    >from right to left. Having that information, I still dont understand in
    >which order the following statements are evaluated :
    >
    >x+++y
    >x++++y
    >x+++++y and so on.
    >I mean how does the compiler decide
    > whether
    >x+++y is x + (++y) or (x++) + y .
    >
    >I am obviously missing something...


    Yup, you've missed the requirement to read the FAQ *before* posting.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 22, 2004
    #6
  7. [snips]

    On Wed, 21 Jul 2004 23:41:44 -0700, Rajorshi wrote:

    > x+++y
    > x++++y
    > x+++++y and so on.
    > I mean how does the compiler decide
    > whether
    > x+++y is x + (++y) or (x++) + y .
    >
    > I am obviously missing something...


    Probably what you're missing is what I refer to as the "maximal munch
    rule": when confronted with a mess such as the above, the compiler creates
    tokens by taking as much as possible at each step. Thus, given "x+++y",
    it will create the following tokens: x, ++, +, y. If it treated the
    "+++" as + followed by ++, it wouldn't be applying the maximal munch rule
    properly; if it treated it as +++, it would be broken, as the code, while
    sloppy, is valid, but the production of a +++ wouldn't be.
     
    Kelsey Bjarnason, Jul 22, 2004
    #7
  8. Kelsey Bjarnason <> writes:
    > [snips]
    > On Wed, 21 Jul 2004 23:41:44 -0700, Rajorshi wrote:
    >
    > > x+++y
    > > x++++y
    > > x+++++y and so on.
    > > I mean how does the compiler decide
    > > whether
    > > x+++y is x + (++y) or (x++) + y .
    > >
    > > I am obviously missing something...

    >
    > Probably what you're missing is what I refer to as the "maximal munch
    > rule": when confronted with a mess such as the above, the compiler creates
    > tokens by taking as much as possible at each step. Thus, given "x+++y",
    > it will create the following tokens: x, ++, +, y. If it treated the
    > "+++" as + followed by ++, it wouldn't be applying the maximal munch rule
    > properly; if it treated it as +++, it would be broken, as the code, while
    > sloppy, is valid, but the production of a +++ wouldn't be.


    And the important thing to remember is that the maximal munch rule
    applies even if a valid parse would otherwise be possible. For
    example:
    x+++++y
    is tokenized as
    x ++ ++ + y
    (which causes a syntax error) even though
    x ++ + ++ y
    would be legal. Tokenization and parsing are separate phases, and
    earlier phases operate without reference to what might happen in later
    ones.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 22, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    104
    Views:
    11,039
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,516
    eliza81
    Jun 11, 2010
  3. Asbjørn Sæbø

    Increment and order of evaluation

    Asbjørn Sæbø, Dec 14, 2007, in forum: C Programming
    Replies:
    6
    Views:
    427
    Ben Bacarisse
    Dec 14, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    904
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    540
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page