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.

ANSI-C syntax determines precedence and associativity (ANSI/ISO 9899-1990, p. 38 and Kernighan/ Ritchie, "The C Programming Language", Second Edition).

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 the above listing, the operator-precedence adds the product of (c*d) to b, and then assigns that sum to a.

In the following listing, the associativity rule 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;