Table of contents
1.
Introduction
2.
Operator Precedence in C++
3.
Example of C++ Operator Precedence
4.
Operator Associativity in C++
5.
Example of C++ Operator Associativity
6.
Operator Precedence Table in C++: 
7.
Frequently Asked Questions 
7.1.
What happens if I use operators with the same precedence in an expression?
7.2.
Can I change the precedence of operators using parentheses?
7.3.
Is it necessary to memorize the entire operator precedence table?
8.
Conclusion
Last Updated: Nov 24, 2024
Easy

Operator Precedence in C++

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Operator precedence is a very important concept in C++ that determines the order in which operators are evaluated in an expression. It is similar to what we call “BODMAS” in mathematics, where we have specific rules to evaluate the expression. In C++, operators with higher precedence are evaluated before operators with lower precedence. This precedence helps programmers understand how the compiler processes complex expressions that contain multiple operators. 

Operator Precedence in C++

In this article, we will discuss the operator precedence, its importance, and how it affects the evaluation of expressions in C++. We will also explain the operator associativity and see an operator precedence table.

Operator Precedence in C++

In C++, operator precedence defines the order in which operators are evaluated when an expression contains multiple operators. It establishes a hierarchy among the operators, specifying which ones have higher or lower precedence. When an expression is evaluated, the operators with higher precedence are applied first, followed by the operators with lower precedence.

For example, consider the expression: 3 + 4 * 5. In this case, the multiplication operator (*) has higher precedence than the addition operator (+). Therefore, the multiplication is performed first (4 * 5 = 20), and then the addition is done (3 + 20 = 23). The result of the expression is 23.

Example of C++ Operator Precedence

Let's take a more complex example to understand operator precedence:

int result = 10 + 3 * 4 - 6 / 2;


In this expression, we have multiple operators: addition (+), multiplication (*), subtraction (-), & division (/). According to the operator precedence rules, the expression is evaluated as follows:

1. Multiplication (*) has higher precedence than addition (+) & subtraction (-), so 3 * 4 is evaluated first, resulting in 12.
 

2. Division (/) also has higher precedence than addition (+) & subtraction (-), so 6 / 2 is evaluated next, resulting in 3.
 

3. Finally, the addition (+) & subtraction (-) operators are applied from left to right. So, 10 + 12 is evaluated, giving 22, & then 22 - 3 is evaluated, resulting in 19.
 

Therefore, the value of the `result` variable will be 19.

Operator Associativity in C++

In addition to operator precedence, C++ also defines operator associativity. Associativity determines the order in which operators with the same precedence are evaluated. There are two types of associativity: left-to-right & right-to-left.

Most operators in C++ have left-to-right associativity, meaning that when multiple operators of the same precedence appear in an expression, they are evaluated from left to right. For example, take the expression mentioned below:

int result = 10 - 5 - 3;


The subtraction operator (-) has left-to-right associativity. So, the expression is evaluated as (10 - 5) - 3, which equals 2.

However, some operators, such as the assignment operator (=) & the ternary conditional operator (?:), have right-to-left associativity. In the case of the assignment operator, this allows chained assignments like:

int a, b, c;
a = b = c = 5;


Here, the assignments are evaluated from right to left. First, c is assigned the value 5, then b is assigned the value of c (which is 5), & finally, a is assigned the value of b (which is also 5).

Example of C++ Operator Associativity

Let's look at an example that shows operator associativity in C++:

int x = 10;
int y = 20;
int z = 30;

int result = x + y * z;

 

In this example, we have two operators: addition (+) & multiplication (*). According to the operator precedence rules, multiplication has higher precedence than addition. However, what happens when we have multiple operators with the same precedence?

The multiplication operator (*) has left-to-right associativity. So, the expression `y * z` is evaluated first, resulting in 600. Then, the addition operator (+) is applied, adding `x` to the result of `y * z`. The final value of `result` will be 610.

Now, let's take another example that takes the assignment operator (=):

int a = 5;
int b = 10;
int c = 15;

a = b = c;


The assignment operator (=) has right-to-left associativity. In this case, the assignments are evaluated from right to left. First, `c` is assigned to `b`, so `b` becomes 15. Then, `b` (which is now 15) is assigned to `a`. After the assignments, both `a` & `b` will have the value 15, while `c` remains unchanged.

Operator Precedence Table in C++: 

Let’s look at a  table that clearly that shows the precedence of various operators from highest to lowest:

PrecedenceOperatorDescription
1::Scope resolution operator
2++, -- (postfix)Suffix/postfix increment and decrement
3++, -- (prefix), +, - (unary), !, ~Prefix increment and decrement, unary plus and minus, logical NOT, bitwise NOT
4*, /, %Multiplication, division, remainder
5+, -Addition, subtraction
6<<, >>Bitwise left shift and right shift
7<, <=, >, >=Relational less than, less than or equal to, greater than, greater than or equal to
8==, !=Equality and inequality
9&Bitwise AND
10^Bitwise XOR
11`` 
12&&Logical AND
13`` 
14?:Ternary conditional
15=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, =Assignment operators
16,Comma operator

Frequently Asked Questions 

What happens if I use operators with the same precedence in an expression?

When operators with the same precedence appear in an expression, their associativity determines the evaluation order. Most operators have left-to-right associativity, but some, like the assignment operator, have right-to-left associativity.

Can I change the precedence of operators using parentheses?

Yes, you can use parentheses to override the default precedence of operators. Expressions inside parentheses are evaluated first, allowing you to control the evaluation order explicitly.

Is it necessary to memorize the entire operator precedence table?

While it's helpful to be familiar with the general precedence rules, memorizing the entire table is not necessary. You can always refer to the table when needed or use parentheses to make the evaluation order clear in your code.

Conclusion

In this article, we discussed the concept of operator precedence and associativity in C++. We learned how precedence determines the order of evaluation for operators in an expression and how associativity helps when operators have the same precedence. We also looked at examples to understand these concepts and provided an operator precedence table. 

You can also check out our other blogs on Code360.

Live masterclass