为什么有这个输出?

发布于 2024-11-04 07:53:33 字数 397 浏览 1 评论 0原文

我一直在尝试理解以下 C 程序:

#include <stdio.h>

int arr[] = {1,2,3,4};
int count;

int incr(){
 return ++count;
}

int main(){  
   arr[count++]=incr();
   printf("%d %d",count,arr[count]);    
   return 0;
}

该程序将 1 2 作为 输出,我不明白的是为什么这里的 count 值是 1 而不是 2 (因为有两个增量)?

I have been trying to understand of this following C-program:

#include <stdio.h>

int arr[] = {1,2,3,4};
int count;

int incr(){
 return ++count;
}

int main(){  
   arr[count++]=incr();
   printf("%d %d",count,arr[count]);    
   return 0;
}

The program gives 1 2 as output,what I am not gtting is why the value of count is 1 here and not 2 (since there are two increments)?

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

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

发布评论

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

评论(2

暗喜 2024-11-11 07:53:33

= 运算符的操作数的求值顺序在 arr[count++]=incr(); 中未指定,并且由于两个操作数都试图修改同一个全局变量 count 根据计算顺序,不同编译器的结果会有所不同。

编辑

实际上,行为是未定义(这意味着任何事情都可能发生),因为“不会访问变量 count 的先前值(仅)确定要存储的值。”

The order of evaluation of operands of = operator is unspecified in arr[count++]=incr(); and since both the operands are trying to modify the same global variable count the result would be different on different compilers depending upon the order of evaluation.

EDIT

Actually the behavior is undefined (which means anything can happen) because "the prior value of the variable count is not accessed (only) to determine the value to be stored."

楠木可依 2024-11-11 07:53:33

incr() 将返回 12。这取决于实现是先递增 count 然后调用 incr(),还是先调用 incr() 然后递增 >计数。

请注意,此选择并不意味着行为未定义。由于在进入函数之前和离开函数之后,每个点都有一个序列点,我们这里的两个增量都被一个序列点分隔开,因此 main 中的增量,如果它在调用之前开始发生,一旦进入 incr() 就会完成,如果它发生在 incr() 被调用之后,直到 才开始发生>incr() 已离开。

我们这里有多种场景:

  • 首先对 count++ 进行增量,然后调用 incr()。这会将 2 写入 arr[0]
  • 首先调用incr(),然后对count++进行增量。这会将 1 写入 arr[1]

因此,count 始终为 2,而 arr[count] 始终为 3(它没有被覆盖) 。所以它应该输出 2 3,而不是 1 2

我认为如果你执行以下操作,你就有更多选择

int main(){  
   arr[++count]=incr();
   printf("%d %d",count,arr[count]);    
   return 0;
}

现在,从 ++count 读取的值可能与 count+1 不同,因为没有什么可以停止incr() 在递增 count 之后但在读取之前调用。在本例中,我们

  • 首先对 ++count 进行增量,然后调用 incr(),然后从 count 读取。这会将 2 写入 arr[2]
  • 首先对++count进行增量,然后从count读取,然后调用incr()。这会将 2 写入 arr[1]
  • 首先调用 incr(),然后对 ++count 进行增量并从中读取。这会将 1 写入 arr[2]

在这种情况下,您可以输出 2 22 12 3

incr() will return either 1 or 2. It depends on whether the implementation first increments count and then calls incr(), or whether it first calls incr() and then increments count.

Please note that this choice does not mean that behavior is undefined. Since before a function is entered, and after a function is left, there is a sequence point at each point, both increments we have here are separated by a sequence point, so that the increment in main, if it started to happen before the call, will be finished once entering incr(), and if it happens after incr() was called, will not have yet started until incr() has left.

We have multiple scenarios here:

  • First do the increment for count++, then call incr(). This will write 2 into arr[0].
  • First call incr(), then do the increment for count++. This will write 1 into arr[1].

So, count is always 2, and arr[count] is always 3 (it wasn't overwritten). So it should output 2 3, not 1 2.

I think that if you do the following, you have more options

int main(){  
   arr[++count]=incr();
   printf("%d %d",count,arr[count]);    
   return 0;
}

Now, the value read from ++count can be different than count+1, because there is nothing that stops incr() to be called after incrementing count but before reading it. In this case we have

  • First do the increment for ++count, then call incr(), then read from count. This will write 2 into arr[2].
  • First do the increment for ++count, then read from count, and then call incr(). This will write 2 into arr[1].
  • First call incr(), then do the increment for ++count and read from it. This will write 1 into arr[2].

In this case, you can either have output 2 2 or 2 1 or 2 3.

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