C++ Operator Precedence

The order in which different type of operators in an expression are evaluated is known as operator precedence. C++ operator precedence is also known as hierarchy of operators.
Each operator has its own precedence level. If an expression contains different types of operators, the operators with higher precedence are evaluated before the operators with lower precedence. The order of precedence in C++ Language is as follows:
  • Any expression given in parentheses is evaluated first.
  • Then Multiplication * and division / Operators are evaluated.
  • Then plush + and minus – operators are evaluated.
  • In case of parentheses within parentheses, the expression of the inner parentheses will be evaluated first.

Example:
The expression 10*(24/(5-2))+13 is evaluated in the following order:
  1. First of all the expression 5-2 will be evaluated. It gives a value 3.
  2. Secondly, 24 will be divided by the result of last line i.e. 24/3 giving value 8.
  3. Thirdly, 10 will be multiplied by 8. i.e. giving result 80.
  4. Finally 80 wil be added in 13 and the last result will be 93.


10*(24/5-2)+13
10*(24/3)+13
10*8+13
80+13
93

Post a Comment

Previous Post Next Post