gpt4 book ai didi

c - C中 bool 表达式的结果

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:03 25 4
gpt4 key购买 nike

为什么下面的表达式求值为 0?

i > --i

假设 i = 5。从左到右计算表达式,我们计算左操作数 (i) 得到 5,计算右操作数 (--i) 得到 4。所以表达式 about 应该计算为 1 . 但是当我用 gcc 编译并运行它时,它总是评估为 0。我的思维过程有缺陷吗?

最佳答案

这只是未定义的行为,因为您正在修改 i 的值以及在没有中间序列点的情况下阅读它。关系运算符 < 引入序列点。

来自 C11,6.5(2):

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

关于c - C中 bool 表达式的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568058/

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