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

K

karthikbalaguru

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
 
P

Peter Nilsson

karthikbalaguru said:
* / %            - 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.
 
K

Keith Thompson

Peter Nilsson said:
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.
 
P

Phil Carmody

Keith Thompson said:
Peter Nilsson said:
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
 
P

Peter Nilsson

Peter Nilsson said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top