Operator Precedence and Associativity in C++
Last Updated :
24 May, 2024
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:
int x = 5 - 17 * 6;
As, multiplication has higher precedence than subtraction, that's why 17 * 6 is evaluated first, resulting in x = -97. If we want to evaluate 5 - 17 first, we can use parentheses:
int x = (5 - 17) * 6;
Now 5 - 17 is evaluated first, resulting in x = -72.
Example of C++ Operator Precedence
C++
#include <iostream>
using namespace std;
int main()
{
// Multiplication has higher precedence
int result = 5 - 17 * 6;
cout << "Result: " << result << endl; // Output: -97
// Because of parentheses 5 - 17 will be calculated
// first
result = (5 - 17) * 6;
cout << "Result: " << result << endl; // Output: -72
return 0;
}
// This code is contributed by Susobhan Akhuli
OutputResult: -97
Result: -72
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: In expressions like a + b - c, the addition and subtraction operators are evaluated from left to right. So, (a + b) - c is equivalent.
Right-to-left associativity: Some operators, like the assignment operator =, have right-to-left associativity. For example, a = b = 4; assigns the value of b to a.
For expression:
int x = 10 - 5 - 2;
As, subtraction is left-associative, that's why 10 - 5 evaluated first, resulting in x = 3. But in case of multiplication:
int x = 2 * 3 * 4;
Now 3 * 4 is evaluated first, resulting in x = 24 because multiplication is right-associative.
Example of C++ Operator Associativity
C++
#include <iostream>
using namespace std;
int main()
{
// Subtraction is left-associative
int result = 10 - 5 - 2;
cout << "Result: " << result << endl; // Output: 3
// Multiplication is right-associative
result = 2 * 3 * 4;
cout << "Result: " << result << endl; // Output: 24
return 0;
}
// This code is contributed by Susobhan Akhuli
OutputResult: 3
Result: 24
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
Operator | Name | Associativity |
---|
() [] -> . | Function call, Subscript, Member access | Left |
++ -- | Increment/Decrement | Right |
! ~ - + | Logical/Bitwise NOT, Unary plus/minus | Right |
* / % | Multiplication, Division, Modulus | Left |
+ - | Addition, Subtraction | Left |
<< >> | Bitwise shift | Left |
< <= > >= | Relational operators | Left |
== != | Equality operators | Left |
& | Bitwise AND | Left |
^ | Bitwise XOR | Left |
| | Bitwise OR | Left |
&& | Logical AND | Left |
|| | Logical OR | Left |
?: | Ternary conditional | Right |
= += -= *= /= %= &= ^= |= <<= >>= | Assignment and compound assignment | Right |
, | Comma | Left |
Similar Reads
Operator Precedence and Associativity in C
Operator precedence and associativity are rules that decide the order in which parts of an expression are calculated. Precedence tells us which operators should be evaluated first, while associativity determines the direction (left to right or right to left) in which operators with the same preceden
7 min read
Operator Precedence and Associativity in Programming
We have many types of operators in a programming language. Operators serve many purposes. But when we have many operators within the same statement, Operator Precedence and Associativity come into the picture. They define which operator should be taken into account first. It is similar to the BODMAS
3 min read
Difference between Operator Precedence and Operator Associativity
In programming, operators are used to perform various operations on data. Understanding how operators interact with each other is crucial for writing correct and efficient code. In this article, we will explore the two most important concepts which are Operator Precedence and Operator Associativity.
3 min read
Operator Associativity in Programming
Operator associative refers to the order in which operators of the same precedence are used in a word. In a programming language, it is important to understand the interactions between operators to properly define and test expressions. In this article, we will discuss operator associativity in progr
14 min read
Operator precedence in JavaScript
Operator precedence refers to the priority given to operators while parsing a statement that has more than one operator performing operations in it. Operators with higher priorities are resolved first. But as one goes down the list, the priority decreases and hence their resolution. ( * ) and ( / )
2 min read
Floating Point Operations & Associativity in C, C++ and Java
Do Floating point operations follow property of associativity? In other words, do we always get the same results for expressions "(A + B) + C" and "A + (B + C)" One may expect that floating numbers to follow the rule of associativity in programming languages as they are associative mathematically. H
4 min read
Precedence of postfix ++ and prefix ++ in C/C++
In C/C++, precedence of Prefix ++ (or Prefix --) has same priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix --) is higher than both Prefix ++ and *. If p is a pointer then *p++ is equivalent to *(p++) and ++*p is equivalent to ++(*p) (both Prefix ++ and * are right ass
4 min read
Bitwise AND operator in Programming
In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we'll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization tech
6 min read
Assignment Operators in C++
In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in
6 min read
Sequence vs Associative containers in C++
Sequence Containers In standard template library they refer to the group of container class template, we use to them store data. One common property as the name suggests is that elements can be accessed sequentially. Each of the following containers use different algorithm for data storage thus for
3 min read