# 计算的优先级和顺序Precedence and Order of Evaluation

C 运算符的优先级和结合性将影响表达式中操作数的分组和计算。The precedence and associativity of C operators affect the grouping and evaluation of operands in expressions. 仅当存在优先级较高或较低的其他运算符时，运算符的优先级才有意义。An operator's precedence is meaningful only if other operators with higher or lower precedence are present. 首先计算带优先级较高的运算符的表达式。Expressions with higher-precedence operators are evaluated first. 也可以通过“绑定”一词描述优先级。Precedence can also be described by the word "binding." 优先级较高的运算符被认为具有更严格的绑定。Operators with a higher precedence are said to have tighter binding.

## C 运算符的优先级和结合性Precedence and Associativity of C Operators

[ ] ( ) . ->[ ] ( ) . ->

++ --（后缀）++ -- (postfix)

sizeof & * + - ~ !sizeof & * + - ~ !

++ --（前缀）++ -- (prefix)

typecaststypecasts 一元Unary 从右到左Right to left
* / %* / % 乘法Multiplicative 从左到右Left to right
+ -+ - 加法Additive 从左到右Left to right
<< >> 按位移动Bitwise shift 从左到右Left to right
< > <= >=< > <= >= 关系Relational 从左到右Left to right
== !=== != 相等Equality 从左到右Left to right
& 按位“与”Bitwise-AND 从左到右Left to right
^ 按位“异或”Bitwise-exclusive-OR 从左到右Left to right
|| 按位“与或” Bitwise-inclusive-OR
&& 逻辑“与”Logical-AND 从左到右Left to right
|||| 逻辑“或”Logical-OR 从左到右Left to right
? :? : 条件表达式Conditional-expression 从右到左Right to left
= *= /= %== *= /= %=

+= -= <<= >>= &=+= -= <<= >>= &=

^= |=^= |=

, 顺序计算Sequential evaluation 从左到右Left to right
1. 运算符按优先级的降序顺序列出。Operators are listed in descending order of precedence. 如果多个运算符出现在同一行或一个组中，则它们具有相同的优先级。If several operators appear on the same line or in a group, they have equal precedence.

2. 所有简单的和复合的赋值运算符都有相同的优先级。All simple and compound-assignment operators have equal precedence.

表达式可以包含优先级相同的多个运算符。An expression can contain several operators with equal precedence. 当多个具有相同级别的这类运算符出现在表达式中时，计算将根据该运算符的结合性按从右到左或从左至右的顺序来执行。When several such operators appear at the same level in an expression, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right. 计算的方向不影响在相同级别包括多个乘法 (\)、加法 (+) 或二进制按位 (& | ^) 运算符的表达式结果。The direction of evaluation does not affect the results of expressions that include more than one multiplication (**), addition (+), or binary-bitwise (& | ^**) operator at the same level. 语言未定义运算的顺序。Order of operations is not defined by the language. 如果编译器可以保证一致的结果，则编译器可以按任意顺序随意计算此类表达式。The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result.

只有顺序计算（）、逻辑“与”（&&）、逻辑“或” (`||`)、条件表达式 (? :) 和函数调用运算符构成序列点，因此，确保对其操作数的计算采用特定顺序。Only the sequential-evaluation (,), logical-AND (&&), logical-OR (`||`), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands. 函数调用运算符是一组紧跟函数标识符的圆括号。The function-call operator is the set of parentheses following the function identifier. 确保顺序计算运算符 (,) 按从左到右的顺序计算其操作数。The sequential-evaluation operator (,) is guaranteed to evaluate its operands from left to right. （请注意，函数调用中的逗号运算符与顺序计算运算符不同，且不提供任何此类保证。）有关详细信息，请参阅序列点(Note that the comma operator in a function call is not the same as the sequential-evaluation operator and does not provide any such guarantee.) For more information, see Sequence Points.

逻辑运算符还确保按从左至右的顺序计算操作数。Logical operators also guarantee evaluation of their operands from left to right. 但是，它们会计算确定表达式结果所需的最小数目的操作数。However, they evaluate the smallest number of operands needed to determine the result of the expression. 这称作“短路”计算。This is called "short-circuit" evaluation. 因此，无法计算表达式的一些操作数。Thus, some operands of the expression may not be evaluated. 例如，在下面的表达式中For example, in the expression

`x && y++`

## 示例Examples

a & b || ca & b || c (a & b) || c(a & b) || c
a = b || ca = b || c a = (b || c)a = (b || c)
q && r || s--q && r || s-- (q && r) || s--(q && r) || s--

p == 0 ?p == 0 ? p += 1: p += 2p += 1: p += 2 ( p == 0 ?( p == 0 ? p += 1 : p ) += 2p += 1 : p ) += 2

`( p == 0 ) ? ( p += 1 ) : ( p += 2 )`