About associativity in 'C'
Ho开发者_JAVA技巧w can i decide or understand whether any statement or expression has left to right or right to left associativity?
=, +=, etc, operators that have '=' in them, is right associated.
All others are left-associated.
Here's a table of C++ operator precedence.
The operator precedence and associativity are the same in C and C++ (modulo operators that don't exist in the former).
Read the standards
EDITED: I don't have the standards myself, but I've found this link supposed to be based on the standard.
Also, quoting Kernighan & Ritchie:
Arithmetic operators associate left to right.
Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. Most C programs rely on these properties.
Operators Associativity
() [] -> . left to right
! ~ ++ -- + - * (type) sizeof right to left
* / % 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
= += -= *= /= %= &= ^= |= <<= >>= right to left
, left to right
Unary & +, -, and * have higher precedence than the binary forms.
精彩评论