When an expression contains multiple operators, the precedence of the operator controls the order in which a single operator is evaluated. For example, an expression x+y*z is evaluated by x+ (Y*Z) because the "*" operator has a higher priority than the "+" operator. This is consistent with the multiplication and subtraction of mathematical operations.
Table 7-1 summarizes the order of precedence for all operators from highest to lowest.
Table 7-1 Precedence order of operators from highest to lowest
Category |
Operator |
Primary operator |
(x) x.y f (x) a[x] x + + x--new type of sizeof checked unchecked |
Unary operator |
+ - ! ~ ++x--x (T) x |
Multiply, divide operator |
* / % |
Add and subtract operator |
+ - |
Shift operator |
<< >> |
Relational operators |
< > <= >= is as |
Equality operator |
== != |
Logic and operators |
& |
Logical XOR OR operator |
^ |
Logical OR operator |
| |
Conditions and operators |
&& |
Condition or operator |
|| |
Conditional operator |
?: |
Assignment operator |
= *=/=%= + = = <<= >>= &= ^= |= |
When an operand appears between two operators with the same priority, the operators are executed from left to right in the order in which they appear.
In addition to assignment operators, all binary operators are left-bound (left-associative), that is, actions are performed in Left-to-right order. For example: X+y+z Press (X+Y) +z for evaluation.
Assignment operators and conditional operators (?:) According to the principle of right engagement (right-associative), operations are performed in Right-to-left order. such as: X=y=z in accordance with x= (Y=Z) for evaluation.
It is recommended that when writing an expression, if you cannot determine the effective order of the operators, then try to use parentheses to ensure the order of operations, so that the program at a glance, and their own programming can be clear thinking.