C语言试题疑问

假定int a=2,b=3;,表达式(b/a*2.0>2.0*a/b)+(++a-b--)的值是?

为什么答案是0而不是1呢?

阅读 1.5k
1 个回答

未定义行为,不要理了。

Expressions/2

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. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.

后一个括号里对 a b 的修改与前一个括号里对 a b 的求值顺序是不确定的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题