序列点和评估顺序

发布于 2024-12-29 18:01:45 字数 360 浏览 3 评论 0原文

我正在阅读 K&R,在评估像 a[i]=i++; 这样的表达式时,我遇到了这个关于行为不确定性的例子; 6.5.2 美元的 C99 规格说明了这一点

在上一个和下一个序列点之间,对象的存储值最多应通过表达式的求值修改一次。此外,应只读先前的值以确定要存储的值。

上面来自 K&R 的例子适用于第一个陈述。请解释一下它是如何在第二次失败的。

标准是否说明了在涉及序列点的情况下子表达式的求值顺序。例如。 a[i++] || b[i++]。我知道这些是从左到右评估的,但是这是如何从上面的陈述中得出的,或者它是否在标准中的某处明确说明?

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

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.

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2025-01-05 18:01:45

标准是否规定了序列点情况下子表达式的求值顺序?

在条件运算符的情况下,求值顺序已明确定义&&以及 || 这就是短路起作用的原因。

它是由c99标准明确规定的。

参考: c99 标准

附件 J:J.1 未指定行为

1 以下内容未指定:
......

子表达式的求值顺序和副作用的顺序
发生,除了为函数调用指定的 ()、&&、||、?: 和逗号
运算符 (6.5)。
......

进一步,
6.5.14 逻辑或运算符

4) 与按位 | 不同运算符,||运算符保证从左到右评估;在第一个操作数求值之后有一个序列点。如果第一个操作数与 0 比较不等于,则不计算第二个操作数。

以及逻辑 AND:

6.5.13 逻辑 AND 运算符

与按位二进制 & 不同的是,运算符,&&运算符保证从左到右评估;
如果计算第二个操作数,则计算之间存在一个序列点
第一和第二操作数。如果第一个操作数比较等于 0,则第二个操作数
不计算操作数。

Does standard says anything about the order of evaluation of sub-expressions in case of the sequence points?

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

It is explicitly specified by the c99 standard.

Reference: c99 Standard

Annex J: J.1 Unspecified behavior

1 The following are unspecified:
.....

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).
.....

Further in,
6.5.14 Logical OR operator

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.

As well as for logical AND:

6.5.13 Logical AND operator

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.

我最亲爱的 2025-01-05 18:01:45

对于问题的第一部分:

该句子适用于由表达式更改的对象,即 i (和 a[i])。因此,i 的先前值应专门用于确定 i 的“新”值。

但表达式“使用”它还确定要写入的数组元素。

背景是,否则会不清楚i表示i在增量之前还是之后的值。

For the first part of the question:

The sentence applies to objects being changed by the expression, i.e. i (and a[i]). So, the prior value of i shall be used exclusively to determine the "new" value for i.

But the expression "uses" it also to determine the array element to be written to.

The background is that otherwise it would be unclear if i denotes i's value before or after the increment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文