代码:
#include <stdio.h>
#include <stdlib.h>
void func(void);
int main(void) {
for (int i = 0; i < 9; ++i) {
func();
}
return EXIT_SUCCESS;
}
void func(void) {
// 变量a、b均具有函数作用域
// 自动存储期(automatic storage duration)
int a = 0;
// 静态存储期(static storage duration)
// 仅在编译时初始化一次;如果不显示地对静态变量进行初始化,它们将被初始化为0
// 本质上并不是所在函数的一个语句
static int b = 0;
++a;
++b;
printf("%d %d\n", a, b);
}
输出:
1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9
原文:http://my.oschina.net/Xwoder/blog/330661