gpt4 book ai didi

c++ - 这个未定义的行为或实现是否已定义?

转载 作者:行者123 更新时间:2023-11-28 00:55:28 28 4
gpt4 key购买 nike

以下是未定义的还是实现定义的:

 int x = 0;
printf("%d%d", ++x, x);

评估参数的顺序是未指定的,所以:

  • 如果首先评估 ++x,则打印 11
  • 如果首先评估 x,它会打印 10

最佳答案

printf("%d%d", ++x, x);

这显然是 C++ 中的未定义行为。

(C++11, 1.9p15) "If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."

C 也一样(强调我的):

(C99, 6.5.p2) "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.72) Furthermore, the prior value shall be read only to determine the value to be stored.73"

请注意,C11 现在与 C++11 中的措辞类似:

(C11, 6.5p2) "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++ - 这个未定义的行为或实现是否已定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657100/

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