简体   繁体   English

如何用优先表证明 C 后缀增量运算符?

[英]How to justify C postfix increment operator with precedence table?

I am working with the C operator precedence table to better understand the operator precedence of C. I am having a problem understanding the results of the following code:我正在使用 C 运算符优先级表来更好地理解 C 的运算符优先级。我在理解以下代码的结果时遇到问题:

int a, b;
a = 1;
b = a++;   // does not seem to follow C operator precedence

Using the precedence table of C operators, I can not explain why with the postfix ++ operator, first the assignment is evaluated and then the increment.使用 C 运算符的优先级表,我无法解释为什么使用后缀++运算符,首先评估赋值然后递增。

The postfix increment operator ( ++ ) has the highest precedence in C and the assignment operator ( = ) has the lowest precedence.后缀递增运算符 ( ++ ) 在 C 中具有最高优先级,而赋值运算符 ( = ) 具有最低优先级。 So in the above code first postfix ++ must executed and then assignment = .所以在上面的代码中,必须先执行 postfix ++然后赋值= Therefore both variables a and b should equal 2 but they don't.因此变量ab都应该等于 2 但它们不是。

Why does the C operator precedence seems not to work with this code?为什么 C 运算符优先级似乎不适用于此代码?

When doesn't the highest precedence of postfix ++ show itself? postfix ++的最高优先级什么时候不显示?

This had nothing to do with precedence.这与优先级无关。 It's a matter of how the postfix ++ operator works.这是后缀++运算符如何工作的问题。

The postfix ++ operator evaluates to the current value of its operand, and has the side effect of incrementing its operand.后缀++运算符计算其操作数的当前值,具有增加其操作数的副作用。 In contrast the prefix ++ operator evaluates to the incremented value of its operand.相反,前缀++运算符的计算结果为其操作数的递增值。

int a, b;
a = 1;
b = a++;   // b is 1, a is 2
b = ++a;   // b is 3, a is 3

This behavior of the postfix ++ operator is documented in section 6.5.2.4p2 of the C standard :后缀++运算符的这种行为记录在C 标准的第 6.5.2.4p2 节中:

The result of the postfix ++ operator is the value of the operand.后缀++运算符的结果是操作数的值。 As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).作为副作用,操作数对象的值会增加(即,将适当类型的值 1 添加到其中)。 See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers.有关约束、类型和转换以及操作对指针的影响的信息,请参阅加法运算符和复合赋值的讨论。 The value computation of the result is sequenced before the side effect of updating the stored value of the operand.结果的值计算在更新操作数的存储值的副作用之前排序。 With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation.对于不确定顺序的函数调用,后缀++的操作是单个评估。 Postfix ++ on an object with atomic type is a read-modify-write operation with memory_order_seq_cst memory order semantics.具有原子类型的对象上的 Postfix ++是具有 memory_order_seq_cst 内存顺序语义的读-修改-写操作。

And the prefix ++ operator is documented in section 6.5.3.1p2:前缀++运算符记录在第 6.5.3.1p2 节中:

The value of the operand of the prefix ++ operator is incremented.前缀++运算符的操作数的值递增。 The result is the new value of the operand after incrementation.结果是递增后操作数的新值。 The expression ++E is equivalent to (E+=1) .表达式++E等价于(E+=1) See the discussions of additive operators and compound assignment for information on constraints, types, side effects, and conversions and the effects of operations on pointers.有关约束、类型、副作用和转换以及操作对指针的影响的信息,请参阅加法运算符和复合赋值的讨论。

Precedence only determines which operators are grouped with which operands during parsing.优先级确定在解析期间哪些运算符与哪些操作数分组。 It does not control order of evaluation.控制评估的顺序。 ++ having a higher precedence than = only means that b = a++ is parsed as b = (a++) rather than (b = a)++ . ++具有比=更高的优先级仅意味着b = a++被解析为b = (a++)而不是(b = a)++

The ++ operator (both unary and postfix forms) has a result and a side effect . ++运算符(一元和后缀形式)具有结果副作用 In the expression b = a++ , the result of a++ is the current value of a - that's what gets assigned to b .在表达式b = a++结果a++是的当前值a -这就是被分配给b The side effect of a++ is to add 1 to a . a++副作用是将 1 添加到a

The order in which the assignment to b and the update to a occur is unspecified .在其中分配的顺序b和更新到a发生是不确定的 The most straightforward is最直接的就是

b <- a
a <- a + 1

but the following is also allowed:但也允许以下内容:

tmp <- a
a <- a + 1
b <- tmp

The result of ++a is the current value of a plus 1, and the side effect is to add 1 to a .的结果++a是的当前值a加1,并且副作用是1添加到a Do not assume that in an expression like b = ++a that a is updated before b .不要假设在像表达式b = ++aa更新前b Again, the order of evaluation could be something like同样,评估顺序可能类似于

b <- a + 1
a <- a + 1

The actual evaluation order depends on your compiler, optimization settings, even the surrounding code.实际的评估顺序取决于您的编译器、优化设置,甚至是周围的代码。

The only operators that force left-to-right evaluation of expressions are the && , ||强制从左到右计算表达式的唯一运算符是&&|| , ?: , and comma operators. , ?:和逗号运算符。

The precedence happens during the parsing .优先级发生在解析过程中。 It means that ++ applies to a , not to b = a .这意味着++适用于a ,而不适用于b = a

But ++ means post incrementation, so performed after a is evaluated to be assigned to b++手段递增,经过这么执行a被评估为被分配给b

If you want both to take the value 2 perform a pre-incrementation:如果您希望两者都取值2执行预增量:

b = ++a;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM