/*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:满星辰
*完成日期:2014年 11月 13日
*版本号:v1.0
*
*问题描述:
*程序输入:
*程序输出:
*/
#include <iostream>
using namespace std;
int f(int n);
int main()
{
cout<<f(5)<<" ";
cout<<f(8)<<endl;
return 0;
}
int f(int n)
{
static int a=2;
int b=0;
a+=n;
b+=a;
return b;
}
预计运行结果是:7 7
实际运行结果是:
#include <iostream>
using namespace std;
int func (int a, int b)
{
static int m=0, i=2;
i+=m+1;
m=i+a+b;
return m;
}
int main()
{
int k=4, m=1, p;
p=func(k, m);
cout<<p<<endl;
p=func(k, m);
cout<<p<<endl;
return 0;
}
预计运行结果是:8 15
实际运行结果是:
学习心得:
静态变量 static int a=2 的意思是 这一次运行时a赋初值为2
后变为7
第二次运行时a的值被赋为 7
而 b 的值在二次运行时仍保持原来的值