为什么有这个输出?
我一直在尝试理解以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
=
运算符的操作数的求值顺序在arr[count++]=incr();
中未指定,并且由于两个操作数都试图修改同一个全局变量count
根据计算顺序,不同编译器的结果会有所不同。编辑
实际上,行为是未定义(这意味着任何事情都可能发生),因为“不会访问变量
count
的先前值(仅)确定要存储的值。”The order of evaluation of operands of
=
operator is unspecified inarr[count++]=incr();
and since both the operands are trying to modify the same global variablecount
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."incr()
将返回1
或2
。这取决于实现是先递增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
。我认为如果你执行以下操作,你就有更多选择
现在,从
++count
读取的值可能与count+1
不同,因为没有什么可以停止incr()
在递增count
之后但在读取之前调用。在本例中,我们++count
进行增量,然后调用incr()
,然后从count
读取。这会将2
写入arr[2]
。++count
进行增量,然后从count
读取,然后调用incr()
。这会将2
写入arr[1]
。incr()
,然后对++count
进行增量并从中读取。这会将1
写入arr[2]
。在这种情况下,您可以输出
2 2
或2 1
或2 3
。incr()
will return either1
or2
. It depends on whether the implementation first incrementscount
and then callsincr()
, or whether it first callsincr()
and then incrementscount
.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 enteringincr()
, and if it happens afterincr()
was called, will not have yet started untilincr()
has left.We have multiple scenarios here:
count++
, then callincr()
. This will write2
intoarr[0]
.incr()
, then do the increment forcount++
. This will write1
intoarr[1]
.So,
count
is always2
, andarr[count]
is always3
(it wasn't overwritten). So it should output2 3
, not1 2
.I think that if you do the following, you have more options
Now, the value read from
++count
can be different thancount+1
, because there is nothing that stopsincr()
to be called after incrementingcount
but before reading it. In this case we have++count
, then callincr()
, then read fromcount
. This will write2
intoarr[2]
.++count
, then read fromcount
, and then callincr()
. This will write2
intoarr[1]
.incr()
, then do the increment for++count
and read from it. This will write1
intoarr[2]
.In this case, you can either have output
2 2
or2 1
or2 3
.