Eric Sosman said:
Some of C's operator precedences are downright screwy, and
I confess to some confusion even after two and a half decades'
experience with the language.
After 25 years of dealing with C operator precedence, and 15
years of teaching about it, this is how I distill the precedence
table to what I think are the bare essentials:
For binary operators:
0. Selection/flow control have highest precedence (. -> () [] )
1. My Dear Aunt Sally is next
2. Assignment is very, very low (only the "lowly" comma operator
is lower)
3. All binary operators associate L-to-R, except assignment
Unary operators:
1. Very high precedence (beaten only by selection/flow control
above) and R-to-L associative (or, alternatively, postfix
has higher precedence)
Those are the things worth memorizing, but I never make students
memorize anything about the precedence table (it is always
available open-book, unless I just post it up somewhere in the
room.)
Any code that cannot be /clearly/ resolved by one of the above
rules should be explicitly parenthesized, whether it needs to or
not. And sometimes even code that /can/ be, should be
parenthesized (strange combinations of My Dear Aunt Sally
subexpressions, for example).
I posit that well-written code should never force someone who
has learned the above rules to have to go scrambling for a
precedence table.