C++_静态局部变量的值在函数结束后不会释放,也不会被外部调用

来源:互联网 发布:龙江网络客服中心电话 编辑:程序博客网 时间:2024/04/28 14:11
/************************************************************************时间:2012年10月17日11:00:32                                          **描述:静态局部变量,函数结束后,内存空间仍被保留,但不能被函数外部调用************************************************************************/# include <iostream>using namespace std;int f(int a)//定义f函数,a为形参{auto int b = 0;//等价于 int b = 0; 定义b为自动变量,f()函数结束时,其内存空间被释放static int c = 3;//定义c为静态局部变量,f()函数结束时,其内存空间保留b = b + 1;c = c + 1;return a + b + c;}int main(){int a = 2, i;for(i = 0; i < 3; i++){cout<<f(a)<<" ";}cout<<endl;return 0;}/**************************在c++6.0中运行的结果是:**--------------------    **7 8 9                   **--------------------    **************************/

原创粉丝点击