Statements and Operators: Operator Precedence
What is it?
Operator precedence in C++ is a set of rules that dictate the order in which operations are performed in expressions that contain more than one operator. C++ operators have different levels of precedence. Operators with higher precedence are performed first, and operators of equal precedence are performed from left to right.
Operator Precedence Chart
The C++ operators are arranged here in decreasing order of precedence:
Precedence | Operator | Description |
---|---|---|
1 | :: |
Scope resolution |
2 | a++ , a-- , typeid , const_cast , dynamic_cast , reinterpret_cast , static_cast |
Postfix increment and decrement, typeid operator, type cast operators |
3 | ++a , --a , +a , -a , ! , ~ , (type) , *a , &a , sizeof |
Prefix increment and decrement, unary plus and minus, logical NOT, bitwise NOT, C-style cast, dereference, address-of, sizeof |
4 | .* , ->* |
Pointer to member |
5 | * , / , % |
Multiplication, division, modulo |
6 | + , - |
Addition, subtraction |
7 | << , >> |
Bitwise shift left, bitwise shift right |
8 | < , <= , > , >= |
Less than, less than or equal, greater than, greater than or equal |
9 | == , != |
Equality, inequality |
10 | & |
Bitwise AND |
11 | ^ |
Bitwise XOR |
12 | ` | ` |
13 | && |
Logical AND |
14 | ` | |
15 | ?: |
Ternary conditional |
16 | = , += , -= , *= , /= , %= , &= , ` |
=, ^=, <<=, >>=` |
17 | , |
Comma |
How to use it
The following steps explain how you can use C++ operator precedence in your code:
- Write an expression containing multiple operators.
- Understand the precedence levels of these operators based on the operator precedence chart.
- The operations will be performed according to the precedence levels of the operators. If parentheses
()
are used, operations within the parentheses will be performed first.
Program code snippet example
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3, c = 4, d = 5;
int result = a + b * c / d; // Here, multiplication and division have higher precedence than addition.
cout << "Result: " << result << endl;
result = (a + b) * c / d; // Parentheses have the highest precedence, so a + b will be calculated first.
cout << "Result: " << result << endl;
return 0;
}
Output:
Result: 4
Result: 5
Important to know
- Operator precedence helps in deciding the grouping of terms in an expression. This allows the expression to be evaluated correctly.
- The parentheses
()
can be used to alter the precedence order. The operations within the parentheses are performed first regardless of operator precedence. - When operators
have the same level of precedence, they are processed according to their associativity, usually from left to right.
Best practices
Use parentheses: Although C++ has defined operator precedence rules, using parentheses to make the order of operations explicit can make the code easier to read and understand. This can also help avoid errors.
Understand operator precedence: Make sure to understand and remember the operator precedence rules to correctly write and interpret complex expressions.
Avoid relying on implicit order of operations: Relying on the implicit order of operations can make the code harder to read and debug. It's better to make the order of operations explicit using parentheses.
Calculate each expression of a
, b
, and c
so a
equals 65, b
equals 15 and c
equals 8.