using namespace std;
static int i = 0;
void fun1() {
i = 0;
i++;
cout << "i:" << i << endl;
}
void fun2() {
static int j = 0;
j++;
cout << "j:" << j << endl;
}
int main(void) {
for (int k = 0; k < 10; k++) {
fun1();
fun2();
cout << "======================" << endl;
}
return 0;
}
当全局变量i被static修饰后,在函数中如果再对其进行初始化那么每一次调用都会将其初始化为0,所以输出的每次结果都为1;在函数中j被static修饰时,只会初始化一次,故每次调用都会使其的值加1。