为什么需要静态变量
1. 保持函数状态
2. 避免全局变量的命名冲突
3. 初始化只发生一次
4. 局部变量的持久性
5. 内存管理
示例代码
#include <stdio.h>
void countCalls() {
static int callCount = 0; // 静态变量
callCount++;
printf("Function has been called %d times.\n", callCount);
}
int main() {
for (int i = 0; i < 5; i++) {
countCalls();
}
return 0;
}Last updated