Operators Precedence in C++ :

In this article, we will discuss the operator’s precedence in C++. We generally solve mathematical expressions which have many operators. Similarly, C++ provides us with different types of operators such as arithmetic operators, logical operators, relational operators, and bit-wise operators, etc. For solving expressions with multiple operators in programming we have to consider the concept of operators precedence.

What is Operators Precedence :

We can say precedence means priority in simple terms. This means when we are solving some expressions which have multiple operators, we are going to provide priority for some operators over others. Which means we evaluate the operators first. We can keep this statement simple that is,

The operators with higher precedence will get evaluated before the operators with lower precedence. C++ follows the following order for the operator’s precedence.

Operators Precedence in C++

Don’t worry about remembering all the operator’s precedence order. Just try to remember the most commonly used operators. In simple, the order is postfix, unary, multiplicative, additive, shift, relational, equality, bitwise and logical, and ternary assignment operators.

There is another concept called associativity, If in an expression there are multiple operators with the same precedence then associativity comes into the picture. It can be either from left to right or from right to left.

If associativity is left to right then the expression should be evaluated from left to right, similarly if associativity for those operators is right to left then that expression should be evaluated from right to left.

From the above operators, unary, ternary(conditional) and assignment have associativity right to left, and remaining all has left to right.

Examples of Operator precedence in C++ :

Let’s see some examples to get a better idea about the operator’s precedence and associativity. Consider the below expressions and evaluate.

  1.  a  =  2 + 3  * 4
  2.  b  =  (2+3) * 4
  3.  c  =  4 * 8 / 2 + 3 % 4

 

  • In the first expression, a value will be 14, since ‘*’ has higher precedence it’s evaluated first. therefore a=2+12 which is 14.
  • In the second expression, the b value is 20 because parenthesis has more precedence, so the expression inside parenthesis is evaluated first so, b = 5*4 which is 20
  • The third expression is a bit complex. It has some operators with the same precedence. So here we apply associativity also, since multiplication(*), dividing(/), and remainder(%) operators have higher precedence than addition(+) operators. They should be evaluated first. Since *, /, % have the same precedence we apply its associativity which is left to right, So, the expression is evaluated from left to right.
           c = 32/2 + 3%4 = 16 + 3%4  = 16+3 = 19

Now let’s see the code in C++. Where we consider the above expressions and display the output.

#include <iostream>
using namespace std;
int main() {
    int a,b,c;
    a = 2 + 3 *4;
    b = (2+3) * 4;
    c = 4 * 8 / 2 + 3 % 4;
    cout<<"a : "<< a <<"\n"<<"b : "<< b <<"\n"<<"c : "<< c <<"\n";
    return 0;
}

Output : 

a : 14
b : 20
c : 19

Hope you have understood everything we have discussed in this article.

Leave a Reply

Your email address will not be published. Required fields are marked *