序列点和评估顺序 [英] Sequence points and order of evaluation

查看:145
本文介绍了序列点和评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读通过K&安培; R和我在行为上碰到这个例子中的不确定性在评估前pression像 A [i] =我++ ;
C99的规格书$ 6.5.2说,

I was reading through K&R and i came across this example about uncertainty in behavior while evaluating expression like a[i]=i++; The C99 spec in $6.5.2 says that

在previous和下一个序列点之间的对象应具有其存储的值由前pression评价修改最多一次。此外,前一个值是只读,以确定该值将被存储

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

从K&放上面的例子; R持有的第一条语句不错。请解释它是如何失败的第二个。

The above example from K&R holds good on the first statement. Please explain how does it fail on the second.

标准是否说,关于子前pressions的评价中被卷入的序列点的情况下,指令任何东西。例如。 A [我++] || B〔我++] 。我知道这些都是从左到右评价,但怎么能这样从上面的语句派生抑或是明确的标准规定的地方?

Does standard says anything about the order of evaluation of sub-expressions in case of the sequence points being involved. Eg. a[i++] || b[i++]. I know that these are evaluated from left to right but how can this be derived from the above statement or is it explicitly stated in the standard somewhere ?

推荐答案

是否标说,大约分前pressions的评价,在序列点的情况下,为了什么?

评估的顺序条件运算符的情况下被很好地定义&放大器;&安培; 以及 || 那就是由于这个原因短路作品。

The order of evaluation is well defined in case of conditional operators && as well as || and that is the very reason short circuiting works.

有明确的C99标准规定。

It is explicitly specified by the c99 standard.

参考: C99标准

附件J:J.1不确定的行为

1下面是不确定的:结果
  .....

1 The following are unspecified:
.....

在SUBEX pressions的计算顺序和顺序副作用
  发生,除了作为函数调用(),放大器指定;&放;, ||,:和逗号?
  运营商(6.5)。结果
  .....

The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call (), &&, ||, ?:, and comma operators (6.5).
.....

此外,在,结果
6.5.14逻辑或运算

4)不同的是按位|操作者,在||左到右的运营商保证评价;还有就是第一个操作数的评估后的序列点。如果第一个操作数比较不等于0,第二个操作数不计算。

4) Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated.

以及针对逻辑与

6.5.13逻辑与运算

不同的是按位二进制和放大器;操作者,在&放大器;&放大器;左到右的运营商保证评价;
  如果第二操作数被计算,存在的评价之间的序列点
  在第一和第二操作数。如果第一操作数进行比较等于0,第二个
  操作数不计算。

Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

这篇关于序列点和评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆