Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

Operator Precedence and Associativity in C++

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

In C++,operator precedence and associativity are important concepts that determine the order in which operators are evaluated in an expression. Operator precedence tells the priority of operators, while associativity determines the order of evaluation when multiple operators of the same precedence level are present.

Operator Precedence in C++

In C++, operator precedence specifies the order in which operations are performed within an expression. When an expression contains multiple operators, those with higher precedence are evaluated before those with lower precedence.

For expression:

10 + 20 * 30

The expression contains two operators, + (addition) and * (multiplication). According to operator precedence, multiplication (*) has higher precedence than addition (+), so multiplication is checked first. After evaluating multiplication, the addition operator is then evaluated to give the final result.

Operator-Precedence-in-C-01

Example of C++ Operator Precedence

C++
#include <iostream>
using namespace std;

int main()
{
    // Multiplication has higher precedence
    int result = 10 + 20 * 30;

    cout << "Result: " << result << endl;
    return 0;
}

Output
Result: 610

Remember that, using parentheses makes code more readable, especially when dealing with complex expressions.

Operator Associativity in C++

Operator associativity determines the order in which operands are grouped when multiple operators have the same precedence. There are two types of associativity:

  • Left-to-right associativity means that when multiple operators of the same precedence appear in an expression, they are evaluated from left to right. For example, in the expression a + b - c, addition and subtraction have the same precedence and are left-associative, so the expression is evaluated as (a + b) - c.
  • Right-to-left associativity means the operators are evaluated from right to left. For example, the assignment operator = is right-associative. So, in the expression a = b = 4, the value 4 is first assigned to b, and then the result of that assignment (b, which is now 4) is assigned to a.

For expression:

100 / 10 % 10

The division (/) and modulus (%) operators have the same precedence, so the order in which they are evaluated depends on their left-to-right associativity. This means the division is performed first, followed by the modulus operation. After the calculations, the result of the modulus operation is determined.

Operator-Associativity-

We can verify the above using the following C++ program:

C++
#include <iostream>
using namespace std;

int main() {
  
    int result = 100 / 10 *10;
    
    cout<<"Result: "<< result<<"\n";
    
    return 0;
}

Output
Result: 100

Operators Precedence and Associativity are two characteristics of operators that determine the evaluation order of sub-expressions.

Example of C++ Operator Precedence and Associativity

In general, operator precedence and associativity are applied together in expressions. Consider the expression exp = 100 + 200 / 10 - 3 * 10, where the division (/) and multiplication (*) operators have the same precedence but are evaluated before addition (+) and subtraction (-). Due to left-to-right associativity, the division is evaluated first, followed by multiplication. After evaluating the division and multiplication, the addition and subtraction are evaluated from left to right, giving the final result.

Operator-Precedence-and-Associativity-in-C

Again, we can verify this using the following C++ program.

C++
#include <iostream>
using namespace std;

int main(){
    
    int result = 100 + 200 / 10 - 3 * 10;
    
    // Verifying the result of the same expression
    cout << "Result: " << result << endl; 

    return 0;
}

Output
Result: 90

Understanding both the precedence and associativity of operators is crucial for writing expressions that produce the desired results.

Operator Precedence Table in C++

The operator precedence table in C++ is a table that lists the operators in order of their precedence level. Operators with higher precedence are evaluated before operators with lower precedence. This table also includes the associativity of the operators, which determines the order in which operators of the same precedence are processed.

The operators are listed from top to bottom, in descending precedence

OperatorNameAssociativity
() [] -> .Function call, Subscript, Member accessLeft
++ --Increment/DecrementRight
! ~ - +Logical/Bitwise NOT, Unary plus/minusRight
* / %Multiplication, Division, ModulusLeft
+ -Addition, SubtractionLeft
<< >>Bitwise shiftLeft
< <= > >=Relational operatorsLeft
== !=Equality operatorsLeft
&Bitwise ANDLeft
^Bitwise XORLeft
|Bitwise ORLeft
&&Logical ANDLeft
||Logical ORLeft
?:Ternary conditionalRight
= += -= *= /= %= &= ^= |= <<= >>=Assignment and compound assignmentRight
,CommaLeft



Operator Precedence and Associativity in C++
Visit Course explore course icon
Article Tags :

Explore