[英]What happens to the increment of “b” in “return b++” ,if used in a function?
Since b++
is post-increment,what happens to the increment of b
if used as return b++
as in the following program? 由于b++
是后递增的,如果用作return b++
, b
的增量会发生什么变化,如下面的程序?
#include<stdio.h>
int foo(int);
int main()
{
int a=8;
printf("%d",foo(a));
}
int foo(int a)
{
static int b=a*a;
return b++;
}
Edit 编辑
#include<stdio.h>
int foo();
int main()
{
foo();
foo();
}
int foo()
{
static int b=1;
printf("%d\n",b);
return b++;
}
Result 结果
1 1
2 2
As I saw in my edit, why is b
incremented at all?Isn't return
supposed to exit that function immediately?Why is b
incremented even after control returns to main()
? 正如我在我的编辑看到了,为什么b
都增加?是不是return
应该立即退出该功能?为什么b
递增即使控制返回到main()
Aren't all activities in the function supposed to end after return? 返回后,函数中的所有活动都不应该结束吗?
Many C (sub-)expression have a value and a side effect. 许多C(子)表达具有值和副作用。
The value of b++
is the value of b
before evaluating the expression; 在评估表达式之前, b++
的值是b
的值; its side effect is to increase the value in b
by one. 它的副作用是将b
的值增加1。
So, the expression return b++;
所以,表达式return b++;
returns the previous value of b
and updates b
. 返回b
的先前值并更新b
。 When b
is static the update stays around for the next function call; 当b
是静态时,更新保持在下一个函数调用; when b
is a plain old local variable, the update is lost (a smart compiler will even not emit code to update the object). 当b
是一个普通的旧局部变量时,更新就会丢失(智能编译器甚至不会发出代码来更新对象)。
Actually, this code doesn't compile, because b
's initializer isn't a constant. 实际上,这段代码不能编译,因为b
的初始化程序不是常量。
Since b
is static, it's essentially a global variable, and it gets initialized before main
, at which point a
doesn't exist. 由于b
是静态的,它本质上是一个全局变量,它在main
之前被初始化,此时a
不存在。
If on the other hand we compile this as C++, b
is initialized the first time that foo
is called - so gets the value 64. At the return, b++
is incremented and stored as 65 - but the return value is 64. So if you call foo
again, it will return 65 (and b
is 66). 另一方面,如果我们将其编译为C ++,则在第一次调用foo
时初始化b
- 因此得到值64.在返回时, b++
递增并存储为65 - 但返回值为64.所以如果你再次调用foo
,它将返回65(并且b
为66)。
Edit based on edited code: 根据编辑的代码进行编辑:
So, the code essentially performs: 所以,代码基本上执行:
int temp = b;
b = b + 1;
return temp;
This is the way that C is define. 这是C定义的方式。 The result of x++
is the previous value x
, but the value of x
is increment. 的结果x++
是以前的值x
,而是值x
是增量。 Since static
is essentially a global (but without a name, so you can't use it outside of that function), the value persists between calls to the function, and it is initialized before main
is called. 由于static
本质上是一个全局的(但是没有名称,所以你不能在该函数之外使用它),该值在函数调用之间持续存在,并且在调用main
之前初始化。
return b++;
is equivalent to: int c = b; b = c + 1; return c;
相当于: int c = b; b = c + 1; return c;
int c = b; b = c + 1; return c;
To answer your new questions: - The function exits after return indeed, but before it returns, the expression b++
must be evaluated first. 回答你的新问题: - 函数在返回之后退出,但在返回之前,必须首先计算表达式b++
。 Evaluating such an expression will result in b
being incremented. 评估这样的表达式将导致b
递增。 - All activities in the function "end" after return but b
is declared as a static variable, in which case its value persists through subsequent executions of the function. - 返回后函数“end”中的所有活动,但b
被声明为静态变量,在这种情况下,它的值会在后续执行函数时持续存在。
To make it easier for you to understand, a post increment is like the following function 为了使您更容易理解,后增量类似于以下函数
int postincrement(int& x) {
int y = x;
x = x + 1;
return y;
}
(Of course the compiler could optimize a b++
if it finds that incrementing b
has no effect at all, but in this case it does have an effect (increment the static int b
) so it can't be optimized out.) (当然,编译器可以优化b++
如果它发现增量b
根本没有效果,但在这种情况下它确实有效(增加静态int b
),因此无法优化它。)
This post-increment is completely useless - because b
has local scope, its incremented value is disposed after return
. 这种后增量完全没用 - 因为b
具有局部范围,其递增值在return
后处理。 If your compiler is smart, it most likely to optimize it out. 如果您的编译器是智能的,那么它最有可能优化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.