For the most part, the operator precedences in C seem to be fairly
reasonable, in that parentheses are not required for many common idioms.
However, one that still bites me occasionally is
if(x&4 == 0)
which doesn't have the intended effect because == has a higher
precedence than &, which seems strange. Any thoughts on why this is so?
It seems to me that the logical comparison operators ==, !=, <, >, >=
and <= should have had lower precedence than the bit operators &, ^, and |.
Not that I'm advocating a change - at this date, that would be completely
impractical.
I've found that I refer to page 215 of Kernighan & Ritchie often enough
that I xeroxed the page and posted it on the wall behind my desk.
Phil Karn
The precedence of & and | were as they are now.
Primarily at the urging of Alan Snyder, the && and || operators were
added. This successfully separated the concepts of bitwise operations and
short-circuit Boolean evaluation. However, I had cold feet about the
precedence problems. For example, there were lots of programs with
things like
if (a==b & c==d) ...
In retrospect it would have been better to go ahead and change the precedence
of & to higher than ==, but it seemed safer just to split & and &&
without moving & past an existing operator. (After all, we had several
hundred kilobytes of source code, and maybe 3 installations....)
Dennis Ritchie