English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about C ++operator precedence and associativity.
If there are multiple operators in a single expression, they will not be evaluated simultaneously. Instead, those withhigher precedenceoperators will first evaluate their operands.
Let's look at an example:
int x = 5 - 17 * 6;
Here, the multiplication operator * has a higher precedence than the subtraction operator - . Therefore,17 * 6first evaluating.
the result, the above expression is equivalent to
int x = 5 - (17 * 6);
if we want to evaluate first5 - 17and must be placed insideinside the parentheses:
int x = (5 - 17) * 6;
#include <iostream> using namespace std; int main() { // first evaluates17 * 6 int num1 = 5 - 17 * 6; //with num1the equivalent expression int num2 = 5 - (17 * 6); //forces the compiler to evaluate5-17 int num3 = (5 - 17) * 6; cout << "num"1 =" << num1 << endl; cout << "num"2 =" << num2 << endl; cout << "num"3 =" << num3 << endl; return 0; }
Output Result
num1 = -97 num2 = -97 num3 = -72
Note:Since C ++There are many operators with multiple precedences in it, so it is strongly recommended that we useparenthesesto make the code more readable.
The following table shows the C ++of the operators. Precedence1represents the highest precedence operator, while the precedence17represents the lowest precedence operator.
precedence | operator | description | associativity |
---|---|---|---|
1 | :: | scope | from left to right |
2 | a++ a-- type( ) type{ } a( ) a[ ] . -> | postfix/postfix increment postfix/postfix decrement function-style cast function-style cast function callindex Accessing the member from the object Accessing the member from the object ptr | from left to right |
3 | ++a --a +a -a ! ~ (type) *a &a sizeof co_await new new[ ] delete delete[] | prefix increment prefix decrement positive negative logical NOT bitwise NOT C-style cast indirect (dereference) get address get size await expression dynamic memory allocation dynamic memory release | from right to left |
4 | .* ->* | member object selector member pointer selector | from left to right |
5 | a * b a / b a % b | multiplication division modulus | from left to right |
6 | a + b a - b | plus minus | from left to right |
7 | << >> | bitwise left shift bitwise right shift | from left to right |
8 | <=> | three-way comparison operator | from left to right |
9 | < <= > >= | less less than or equal greater greater than or equal | from left to right |
10 | == != | equal not equal | from left to right |
11 | & | bitwise and | from left to right |
12 | ^ | bitwise xor | from left to right |
13 | | | bitwise or | from left to right |
14 | && | logical and | from left to right |
15 | || | logical or | from left to right |
16 | a ? b : c throw co_yield = += -= *= /= %= <<= >>= &= ^= |= | ternary conditional operator throw operator yield expression (C++ 2(0) assignment addition assignment subtraction assignment multiplication assignment division distribution modulus assignment bitwise left shift assignment bitwise right shift assignment bitwise and assignment bitwise xor assignment bitwise or assignment | from right to left |
17 | , | comma operator | from left to right |
associationThe property of this will be discussed later.
The associativity of an operator is the calculation of the expression'sdirection. For example,
int a = 1; int b = 4; // a = 4 a = b;
Please see a=4; statement. The associativity of the equal (=) operator is from right to left. Therefore, the value of b is assigned to a.
Similarly, multiple operators can have the same precedence (as shown in the table above). When multiple operators with the same precedence are used in an expression, they will be evaluated based on theirassociativityevaluate them.
int a = 1; int b = 4; b += a -= 6;
operator+and-The equal (=) operator has the same precedence. Since the associativity of these operators is from right to left, this is the evaluation method of the last statement.
a -= 6 will be evaluated first. Therefore, a will be-5
Then, b += -5will be evaluated. Therefore, b will be-1
#include <iostream> using namespace std; int main() { int a = 1; int b = 4; // a -= 6 First, evaluate b += a -= 6; cout << "a = " << a << endl; cout << "b = " << b; }
Output Result
a = -5 b = -1