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

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

1. ### karthikbalaguruGuest

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 ?

Karthik Balaguru

karthikbalaguru, Sep 20, 2009

2. ### Peter NilssonGuest

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

3. ### Keith ThompsonGuest

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
4. ### Phil CarmodyGuest

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
5. ### Peter NilssonGuest

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