The C Programming Language, Ansi C
(indirection
through a pointer) and
&
(address of an object), and
Chapter 3
discusses the comma operator.
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.
Table 2.1: Precedence and Associativity of Operators
Note that the precedence of the bitwise operators &, ^, and | falls below == and !=. This implies that bit-testing expressions like
if ((x & MASK) == 0) ...
must be fully parenthesized to give proper results.
C, like most languages, does not specify the order in which the operands of an operator are evaluated. (The exceptions are &&, ||, ?:, and '.) For example, in a statement like
x = f() + g();
f
may be evaluated before
g
or vice versa; thus if either
f
or
g
alters a variable on which the other depends,
x
can depend on the order of evaluation. Intermediate results can
be stored in temporary variables to ensure a particular sequence.
Similarly, the order in which function arguments are evaluated is not specified, so the statement
printf("%d %dn", ++n, power(2, n)); /* WRONG */
can produce different results with different compilers, depending on whether
n
is incremented before
power
is called. The solution, of
course, is to write
++n;
printf("%d %dn", n, power(2, n));