
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Operators with Precedence and Associativity
In C++, operator precedence and associativity both work together to define the order of evaluation in an expression. Order precedence determines which operators need to be evaluated first according to precedence, whereas associativity defines the direction of operators to be evaluated of the same precedence.
Operator precedence
Operator precedence refers to the order of operations to be performed in expressions when multiple operators are present; here, operators with higher precedence (priority) are calculated first.
For example:
int x = 5 + 17 * 5;
Here, according to operator precedence, multiplication has higher precedence than addition, therefore, it will first get multiplied by 17*5 and then add 5, resulting in the final value equal to 90.
int x = (5+17) *5;
In this case, parentheses have higher precedence than multiplication, therefore, it will first evaluate the value inside parentheses, which is equal to 22 (17 + 5), and then multiply it by 5. Therefore, we will get the final value equal to 110.
Example Code
Here is the following example code showcasing the working of operator precedence:.
#include <iostream> using namespace std; int main() { int x1 = 5 + 17 * 5; cout << "Result 1: " << x1 << endl; int x2 = (5 + 17) * 5; cout << "Result 2: " << x2 << endl; return 0; }
Output
Result 1: 90 Result 2: 110
Operator associativity
Operator associativity determines the direction of operators when two operators of the same precedence are present in an expression.
There are two types of operator associativity:
Left-to-Right (Left Associative): Here, the evaluation is done moving from left to right when operators with the same precedence are present.
Arithmetic operators and comparison operators are left associative.
int x = 20-10-5;
In this first 20-10 will be evaluated, resulting in 10, and then it will be subtracted with 5. So we will get the final value of x = 5.
Right-to-Left (Right Associative): Here, the evaluation is done moving from right to left when operators with the same precedence are present.
Assignment, ternary, and unary operators are right-associative.
Int a = 1; Int b = 4; b += a -= 6;
Here, += and -= are assignment operators. So, according to the right associative a -=6 will be evaluated first, resulting in -5. Then, b+=-5 will be evaluated, giving the final result equal to -1.
Example
Here is the following example code showcasing the working of operator associativity:
#include <iostream> using namespace std; int main() { // Left-to-Right (Left Associative) int x = 20 - 10 - 5; cout << "Result 1: " << x << endl; // Right-to-Left (Right Associative) int a = 1; int b = 4; b += a -= 6; cout << "Result 2: " << b << endl; return 0; }
Output
Result 1: 5 Result 2: -1
Table
Here is the following table of operators, which is arranged from top to bottom with the highest precedence to the lowest precedence at the bottom.
Category | Operator | Associativity |
Primary Expressions | () [] -> . ++(postfix), - -(postfix) | Left to right |
Unary operators | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |