Introduction to Operator Precedence And Associativity in C++:: Part-A-14



Operator Precedence

C++ has a rich repertoire of operators. Since an expression may include several operators, it is important to know in what order the evaluations of the operators occurs. We are already familiar with the precedence of ordinary arithmetic operators: the * , / , and % operators have higher precedence then the + and – operators; i.e., they are evaluated first. For example,

                          42  –  3*5

is evaluated as

                           42  –  (3*5)  = 42  –  15  =  27

Moreover, all the arithmetic operators have higher precedence than the assignment and output operators. For example, the statement

                           n =  42  –  3*5;

will assign the value 27 to n. First the operator * is invoked to evaluate 3*5, then the operator – is invoked to evaluate  42 – 15, and then the operator = is invoked to assign  27 to n.

                                                       Table: Some C++ operators

Operator Description Precedence Associativity Arity Example
Negate 15 Right Unary -n
* Multiply 13 Left Binary m*n
/ Divide 13 Left Binary m/n
% Remainder, modulo 13 Left Binary m%n
+ Add 12 Left Binary m + n
Subtract 12 Left Binary m –n
<< Bit shift left, output 11 Left Binary cout <<  n
= Simple assignment 2 Right Binary m = n

It lists 8 operators that apply to integer variables. They fall into five distinct precedence levels. For example, the unary negate operator  –  has precedence level 15, and the binary multiply operator  *  has precedence level  13, so negative is evaluated before multiply. Thus the expression  m* -n is evaluated as  m* (n). Assignment operators have lower precedence than nearly all other operators, so they are usually performed last.

The column labeled “ Associativity” tells what happens when several different operators with the same  precedence level appear in the same expression. For example  +  and  –  both have precedence level  12 and are left associative, so the operators are evaluated form left to right. For example, in the expression

                           8  –  5  +  4

first 5 is subtracted from 8 and then 4 is added to that sum:

                            ( 8  –  5 )  +  4  =  3  +  4  =  7

The column labeled “Arity” lists whether the operator is unary or binary. Unary means that the operator takes only one operand. For example, the post-increment operator  ++  is unary:  n++ operates on the single variable  n. Binary means that the operator takes two operands. For example, the add operator  +  is binary:  m + n operates on the two variables  m  and  n.

Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond

                             —————– Thanks everyone


About Author


1 Comment

  1. Avatar
    Raju Chakraborty on

    There are three types of operator Unary,binary & ternary. ternary operator is like ?: this operator like if else.
    (confition)? true result : false result

Leave A Reply