Operator precedence

utzoo!decvax!harpo!eagle!karn utzoo!decvax!harpo!eagle!karn
Thu Oct 21 12:18:33 AEST 1982


I might as well throw in something to kick off discussions in this new
group.

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



More information about the Comp.lang.c mailing list