Node:Parentheses and Priority, Next:Unary Operator Precedence, Previous:Expressions, Up:Expressions and operators
Parentheses and Priority
Just as in algebra, the C compiler considers operators to have certain priorities, and evaluates, or parses, some operators before others. The order in which operators are evaluated is called operator precedence or the order of operations. You can think of some operators as "stronger" than others. The "stronger" ones will always be evaluated first; otherwise, expressions are evaluated from left to right.
For example, since the multiplication operator *
has a higher
priority than the addition operator +
and is therefore evaluated
first, the following expression will always evaluate to 10 rather
than 18:
4 + 2 * 3
However, as in algebra, you can use parentheses to force the program
to evaluate the expression to 18:
(4 + 2) * 3
The parentheses force the expression (4 + 2)
to be evaluated
first. Placing parentheses around 2 * 3
, however, would have no
effect.
Parentheses are classed as operators by the compiler; they have a value,
in the sense that they assume the value of whatever is inside them. For
example, the value of (5 + 5)
is 10.