Loading...
C++ Operator Precedence and Associativity

C++ Operator Precedence and Associativity

In this tutorial, we will learn about the precedence and associativity of operators in C++ with the help of examples.


Operator Precedence

Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence.

If there are multiple operators in a single expression, the operations are not evaluated simultaneously. Rather, operators with higher precedence have their operations evaluated first.

Let us consider an example:

int x = 2 + 6 * 4; 

Here, the multiplication operator * is of higher level precedence than the addition operator +. Hence, 6 * 4 is evaluated first.

As a result, the above expression is equivalent to

int x = 2 + (6 * 4);

If we wish to evaluate 2 + 6 first, then we must enclose them within parentheses:

int x = (2 + 6) * 4;

Example 1: C++ Program to understand Operators Precedence

#include <stdio.h>
using namespace std;
int main() {

  // evaluates 6 * 4 first
  int expression1 = 2 + 6 * 4;

  // equivalent expression to num1
  int expression2 = 2 + (6 * 4);

  // forcing compiler to evaluate 2 + 6 first
  int expression3 = (2 + 6) * 4;

  cout << "Expression 1 = " << expression1 << endl;
  cout << "Expression 2 = " << expression2 << endl;
  cout << "Expression 3 = " << expression3 << endl;

  return 0;
}

Output

Expression 1 = 26
Expression 2 = 26
Expression 3 = 32

Note: Because there are a lot of operators in C++ with multiple levels of precedence, it is highly recommended that we use parentheses to make our code more readable.


C++ Operators Precedence Table

The following table (taken from cppreference.com) shows the precedence of C++ operators. Precedence Level 1 signifies operators of highest priority, while Precedence Level 17 signifies operators of the lowest priority.

The property of associativity will be discussed shortly.

Precedence Operator Description Associativity
1 :: Scope Resolution Left to Right
2 a++
a--
type( )
type{ }
a( )
a[ ]
.
->
Suffix/postfix increment
Suffix/postfix decrement
Function cast
Function cast
Function call
Subscript
Member access from an object
Member access from object ptr
Left to Right
3 ++a
--a
+a
-a
!
~
(type)
*a
&a
sizeof
co_await
new new[ ]
delete delete[ ]
Prefix increment
Prefix decrement
Unary plus
Unary minus
Logical NOT
Bitwise NOT
C style cast
Indirection (dereference)
Address-of
Size-of
await-expression
Dynamic memory allocation
Dynamic memory deallocation
Right to Left
4 .*
->*
Member object selector
Member pointer selector
Left to Right
5 a * b
a / b
a % b
Multiplication
Division
Modulus
Left to Right
6 a + b
a - b
Addition
Subtraction
Left to Right
7 <<
>>
Bitwise left shift
Bitwise right shift
Left to Right
8 <=< Three-way comparison operator Left to Right
9 <
<=
>
>=
Less than
Less than or equal to
Greater than
Greater than or equal to
Left to Right
10 ==
!=
Equal to
Not equal to
Left to Right
11 & Bitwise AND Left to Right
12 ^ Bitwise XOR Left to Right
13 | Bitwise OR Left to Right
14 && Logical AND Left to Right
15 || Logical OR Left to Right
16 a ? b : c
throw
co_yield
=
+=
-=
*=
/=
%=
<<=
>>=
&=
^=
|=
Ternary Conditional
throw operator
yield expression (C++ 20)
Assignment
Addition Assignment
Subtraction Assignment
Multiplication Assignment
Division Assignment
Modulus Assignment
Bitwise Shift Left Assignment
Bitwise Shift Right Assignment
Bitwise AND Assignment
Bitwise XOR Assignment
Bitwise OR Assignment
Right to Left
17 , Comma operator Left to Right

C++ Operators Associativity

Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.

For example: ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.

Operator associativity is the direction from which an expression is evaluated. For example,

int a = 2;
int b = 4;

// a will be 4
a = b;

Take a look at a = 4; statement. The associativity of the = operator is from right to left. Hence, the value of b is assigned to a, and not in the other direction.

Also, multiple operators can have the same level of precedence (as we can see from the above table). When multiple operators of the same precedence level are used in an expression, they are evaluated according to their associativity.

int a = 2;
int b = 4;
b += a -= 6;

Both operators += and -= operators have the same precedence. Since the associativity of these operators is from right to left, here is how the last statement is evaluated.

  • a -= 6 is evaluated first. Hence, a will be -5
  • Then, b += -5 will be evaluated. Hence, b will be -1

Example 2: C++ Program to Understand Operators Associativity

We can convert a char array to double by using the std::atof() function.

For Example :-

#include <stdio.h>
using namespace std;

int main() {
  int a = 2;
  int b = 4;

// a -= 6 is evaluated first
  b += a -= 6;

  cout << "a = " << a << endl; ;
  cout << "b = " << b;
}

Output

a = -4
b = 0

We hope that this tutorial helped you develop better understanding of the concept of Operator Precedence and Associativity in C++.

Keep Learning : )


- Related Topics