Precedence and Associativity of Operators for ANSI-C

The following table gives an overview of the precedence and associativity of operators.

Table 1. ANSI-C Precedence and Associativity of Operators
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
Note: Unary +, - and * have higher precedence than the binary forms.

The precedence and associativity is determined by the ANSI-C syntax (ANSI/ISO 9899-1990, p. 38 and Kernighan/ Ritchie, "The C Programming Language", Second Edition, Appendix Table 2-1).

Listing: Examples of operator precedence and associativity
  if (a == b&&c) and
  if ((a == b)&&c) are equivalent.
However,
  if (a == b|c)
is the same as
  if ((a == b)|c)

  a = b + c * d;

In above listing, operator-precedence causes the product of (c*d) to be added to b, and that sum is then assigned to a.

In the following listing, the associativity rules first evaluates c+=1, then assigns b to the value of b plus (c+=1), and then assigns the result to a.

Listing: Three assignments in one statement
a = b += c += 1;