gpt4 book ai didi

c - 为什么 "if (i++ && (i == 1))"是假的,而我是一个值为 1 的整数?

转载 作者:太空狗 更新时间:2023-10-29 16:19:32 27 4
gpt4 key购买 nike

{
int i = 1;
if (i++ && (i == 1))
printf("Yes\n");
else
printf("No\n");
}

根据我的理解,在 if 条件下,首先表达式 (i==1) 将被评估,它应该返回 1 , 然后它与 1 逻辑与,这是 i 的值,所以表达式应该返回 1 && 1 == 1,但是else 部分被执行。

有人可以解释一下为什么执行 else 部分吗?

最佳答案

没有。在 C 中,有一个 sequence point &&的LHS评估之间运算符和 RHS 的评估,增量必须在评估 RHS 之前发生并完成。所以,i++ (相当于 i++ != 0 )被执行并完成增量(并且表达式的计算结果为真),因此在计算 RHS 时,i == 2因此整体表达式是错误的,你得到“否”打印。如果 && 的 LHS运算符评估为 false (0),则不会评估 RHS,因为 && 的“短路”属性运营商。

只有少数运算符具有在 LHS 和 RHS 的评估之间具有序列点的属性:&& , || , 和 , (作为运算符,而不是作为参数列表中的分隔符)— 还有 ? :它也不是二元运算符,但在条件求值之后和 ? 之后的表达式之前有一个序列点或 : 之后的表达式被评估(其中一个或另一个,但不是两者,总是被评估)。

&&||运算符是唯一具有“短路”属性的运算符。 && 的 RHS仅在 LHS 评估为真时才评估; || 的 RHS仅在 LHS 评估为 false 时评估。


序列点说明

Iwillnotexist Idonotexist正确asserted :

The C11 standard hasn't done away with sequence points, only the C++11 standard did.

C++11(ISO/IEC 14882:2011)说:

1.9 Program execution

¶13 Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a single thread (1.10), which induces a partial order among those evaluations. Given any two evaluations A and B, if A is sequenced before B, then the execution of A shall precede the execution of B. If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced. [Note: The execution of unsequenced evaluations can overlap. —end note] Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which. [Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. —end note]

术语“序列点”根本没有出现在 C++11 中(唯一接近匹配的是“序列指针”)。

C11(ISO/IEC 9899:2011)说:

5.1.2.3 Program execution

¶3 Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a single thread, which induces a partial order among those evaluations. Given any two evaluations A and B, if A is sequenced before B, then the execution of A shall precede the execution of B. (Conversely, if A is sequenced before B, then B is sequenced after A.) If A is not sequenced before or after B, then A and B are unsequenced. Evaluations A and B are indeterminately sequenced when A is sequenced either before or after B, but it is unspecified which.13) The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B. (A summary of the sequence points is given in annex C.)

13) The executions of unsequenced evaluations can interleave. Indeterminately sequenced evaluations cannot interleave, but can be executed in any order.

因此,C11 确实保留了序列点,但使用与 C++11 基本相同的术语添加了“之前排序”和相关术语。

关于c - 为什么 "if (i++ && (i == 1))"是假的,而我是一个值为 1 的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com