#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void other()
{
static int a=2;//静态局部变量,具有全局寿命,局部作用域,只在第一次进入函数时被初始化
int c=10;//局部变量,每次进入函数时都初始化
cout<<a<<" "<<c<<endl;
a+=2;
c+=10;
}
int _tmain(int argc, _TCHAR* argv[])
{
other();
other();
system("Pause");
return 0;
}
带有static的变量只在第一次声明的时候被初始化,而不带static的变量每次声明都会被初始化;
故上面代码的结果是a值变了而c值不会改变。