To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial y = ax2 + bx + c:
The circled numbers indicate the order in which C++ applies the operators. There is no arithmetic operator for exponentiation in C++, so we’ve represented x2 as x * x
. In Chapter 5, we’ll discuss the standard library function pow
(“power”) that performs exponentiation.
Suppose variables a
, b
, c
and x
in the preceding second-degree polynomial are initialized as follows: a = 2
, b = 3
, c = 7
and x = 5
. Figure 2.8 illustrates the order in which the operators are applied and the final value of the expression.
18.119.17.64