C 中的表达式求值

发布于 2024-11-26 12:25:43 字数 231 浏览 1 评论 0原文

为什么下面这段 C 代码会打印 12 12 12

int main(int argc, char const *argv[]) {
  int a = 2, *f1, *f2;
  f1 = f2 = &a;
  *f2 += *f2 += a += 2.5;
  printf("%i %i %i\n", a, *f1, *f2);
  return 0;
}

Why does the following piece of C code print 12 12 12

int main(int argc, char const *argv[]) {
  int a = 2, *f1, *f2;
  f1 = f2 = &a;
  *f2 += *f2 += a += 2.5;
  printf("%i %i %i\n", a, *f1, *f2);
  return 0;
}

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

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

发布评论

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

评论(3

余生一个溪 2024-12-03 12:25:43
*f2 += *f2 += a += 2.5;

此行具有 未定义行为,因为您更改了 *f2(即 a) 在同一表达式中多次出现,且没有插入序列点。 UB 意味着你的程序可能会打印“Hello World”,它可能会崩溃,它可能会打印 12 12 1212 12 1029 或者它可能会开始吃掉你的大脑。不要依赖未定义的行为。

引用 C++ 标准(我知道问题被标记为 C,但我没有 C 标准,我知道 C 中也有同样的规则)

除非另有说明,各个运算符的操作数和各个子表达式的求值顺序
表达式以及副作用发生的顺序未指定。53)在之前的之间
标量对象的下一个序列点应通过评估最多修改一次其存储值
的一个表达式。此外,应仅访问先前值以确定要存储的值。
对于完整的子表达式的每个允许的排序,应满足本段的要求
表达;否则,行为未定义

*f2 += *f2 += a += 2.5;

This line has Undefined Behavior because you change the value of *f2(i.e. a) more than once within the same expression without an intervening sequence point. UB means that your program may print "Hello World", it may crash, it may print 12 12 12 or 12 12 1029 or it may start eating your brains. Don't rely on undefined behavior.

To quote the C++ standard ( I know the question is tagged C, but I don't have a C standard by me and I know the same rule holds in C)

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual
expressions, and the order in which side effects take place, is unspecified.53) Between the previous
and next sequence point a scalar object shall have its stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full
expression; otherwise the behavior is undefined.

牛↙奶布丁 2024-12-03 12:25:43

它为所有变量打印出相同的值,因为您只指向一个 int 变量:a

它打印出 12 因为 a + 2.5 = 4a 是一个 int),然后添加它对自己两次。


@Downvoters:为什么这么消极?我认为我的回答说明了这个编译器这个示例代码上做了什么,这应该有助于OP理解行为。我同意 Armen Tsirunyan 的答案是正确的(即应该打勾)并且 行为未根据标准。但标准已经实现,我还没有看到编译器编译代码,然后在运行时突然举手并说未定义的行为!

It prints out the same value for all because you are only pointing to one int variable: a.

It prints out 12 because a + 2.5 = 4 (a is an int), and then you add it to itself twice.


@Downvoters: Why so negative? I think my answer says what this compiler is doing on this example code, which should help the OP understand the behaviour. I agree that Armen Tsirunyan's answer is right (i.e. should get the tick) and that the behaviour is undefined according to the standard. But standards are implemented, and I've yet to see a compiler that compiles code and then, at run-time, suddenly throws up its hands and says Undefined behaviour!.

等往事风中吹 2024-12-03 12:25:43

我因为 *f2*f1 指向 a (一个整数)。

所以 *f2 = &a = 2*f1 = &a = 2

此时,您将值添加到 2.5 (因为 a 是一个整数,所以您将获得 4)。

比你

a = 4
f2 = 4
f1 = 4

现在做的f2+f1+a = 12。

I because *f2 and *f1 point to a (an integer).

So *f2 = &a = 2 and *f1 = &a = 2

At this point you add to a the value of 2.5 (because a is an integer, you'll obtain 4).

Than you have

a = 4
f2 = 4
f1 = 4

At this point you do f2+f1+a = 12.

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