Order of precedence - In combination with '=' and without '='

Discussion in 'C Programming' started by karthikbalaguru, Sep 20, 2009.

  1. Hi,

    I have a query w.r.t order of precedence.
    Lemme start from the below list. (I have not considerred the () []
    -> . sizeof etc that come above the below list)

    * / % - Left to Right
    + - - Left to Right
    << >> - Left to Right
    < <= > >= - Left to Right
    == != - Left to Right
    & - Left to Right
    ^ - Left to Right
    | - Left to Right
    && - Left to Right
    || - Left to Right
    ?: - Left to Right
    = += -= *= /= %= &= ^= |= <<= >>= - Right to Left

    But, here is my query w.r.t the last line in the above list
    in comparison with the earlier lines .
    Why does the >>= has higher priority compared to <<=,
    while << has higher priority compared to >> ?

    Why does |= has higher priority compared to ^= , while
    ^ has higher priority compared to | ?

    I find the below order of precedence
    &= > %= > /= > *=
    while,
    * > / > % > &

    Further, i also find that
    -= > +=
    while,
    + > -

    Why does the precedence/priority get changed/reversed
    when combined with '=' ?
    Any ideas ?

    Thx in advans,
    Karthik Balaguru
     
    karthikbalaguru, Sep 20, 2009
    #1
    1. Advertising

  2. karthikbalaguru <> wrote:
    > * / %            - Left to Right
    > + -               -  Left to Right
    > << >>          -  Left to Right
    > < <= > >=    -  Left to Right
    > == !=           -  Left to Right
    > &                 -  Left to Right
    > ^                  -  Left to Right
    > |                   -  Left to Right
    > &&               -  Left to Right
    > ||                  -  Left to Right
    > ?:                  -  Left to Right
    > = += -= *= /= %= &= ^= |= <<= >>=  - Right to Left
    >
    > Why does the >>= has higher priority compared to <<=,


    It doesn't.

    > while << has higher priority compared to >>   ?


    It doesn't. Operators on the same line have the same
    precedence. It's their association that changes.

    It makes sense for assignment operators ([op]=) to associate
    from right to left. Consider...

    a = b = c + 4;

    Should a be set to c + 4, or should a be assigned b's value,
    then b assigned c + 4?

    Now consider subtraction. What should a - b - c yield?
    Should it be (a - b) - c, or a - (b - c)?

    For most operators, left to right association makes sense.
    For assignment, right to left makes sense.

    --
    Peter
     
    Peter Nilsson, Sep 21, 2009
    #2
    1. Advertising

  3. Peter Nilsson <> writes:
    [...]
    > It makes sense for assignment operators ([op]=) to associate
    > from right to left. Consider...
    >
    > a = b = c + 4;
    >
    > Should a be set to c + 4, or should a be assigned b's value,
    > then b assigned c + 4?

    [...]

    Association doesn't dictate order of evaluation.

    If "=" associated left-to-right, then

    a = b = c + 4;

    would be equivalent to

    (a = b) = c + 4;

    which would be illegal, since (a = b) doesn't yield an lvalue.

    You're right that right-to-left association makes sense for assignment
    operators, but I think you've got the rationale wrong.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 21, 2009
    #3
  4. karthikbalaguru

    Phil Carmody Guest

    Keith Thompson <> writes:
    > Peter Nilsson <> writes:
    > [...]
    >> It makes sense for assignment operators ([op]=) to associate
    >> from right to left. Consider...
    >>
    >> a = b = c + 4;
    >>
    >> Should a be set to c + 4, or should a be assigned b's value,
    >> then b assigned c + 4?

    > [...]
    >
    > Association doesn't dictate order of evaluation.
    >
    > If "=" associated left-to-right, then
    >
    > a = b = c + 4;
    >
    > would be equivalent to
    >
    > (a = b) = c + 4;
    >
    > which would be illegal, since (a = b) doesn't yield an lvalue.


    Ah, but if '=' associated left-to-right, then (a = b) would yield
    an lvalue! Why stop the hypotheticals at just one?

    > You're right that right-to-left association makes sense for assignment
    > operators, but I think you've got the rationale wrong.


    Almost certainly.
    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
     
    Phil Carmody, Sep 21, 2009
    #4
  5. On Sep 21, 10:27 am, Keith Thompson <> wrote:
    > Peter Nilsson <> writes:
    > > [...]

    > If "=" associated left-to-right, then
    >
    >     a = b = c + 4;
    >
    > would be equivalent to
    >
    >     (a = b) = c + 4;
    >
    > which would be illegal, since (a = b) doesn't yield an
    > lvalue.


    That is incidental. It does yield an lvalue in C++ and I can't
    see a particularly need for it _not_ to yield an lvalue in C.
    But then, I can't see a particular need for it _to_ yield an
    lvalue in C either, other than obfuscations like...

    (struct_var_a = struct_var_b).member_c = 42;

    > You're right that right-to-left association makes sense for
    > assignment operators, but I think you've got the rationale
    > wrong.


    Even if there is another rule in C that precludes it, it is
    still worth thinking in the abstract. That abstraction is much
    more illustrative (particularly to the OP) of the problems of
    left to right assignment association, than a lack of lvalue in
    the result!

    --
    Peter
     
    Peter Nilsson, Sep 21, 2009
    #5
    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:
    26
    Views:
    671
    Mark McIntyre
    Jun 15, 2007
  2. vjay
    Replies:
    7
    Views:
    382
    Robin Kåveland Hansen
    Nov 27, 2007
  3. Replies:
    3
    Views:
    312
    Paavo Helde
    Mar 19, 2008
  4. Snorri
    Replies:
    1
    Views:
    287
    Victor Bazarov
    Mar 19, 2008
  5. Meske Moday

    Order of precedence and associativity

    Meske Moday, Jun 15, 2010, in forum: C Programming
    Replies:
    22
    Views:
    717
    David Thompson
    Jul 2, 2010
Loading...

Share This Page