【发布时间】:2021-12-24 18:42:05
【问题描述】:
我学了一些 C 语言并偶然发现了静态变量的解释。 他们展示了这段代码:
#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
我不明白为什么调用该函数两次就可以了,因为行
static int count = 0;
实际上运行了两次... 我无法理解这怎么可能... 你真的可以声明它两次还是编译器第二次忽略它?
【问题讨论】:
-
静态变量初始化只在函数第一次被调用时发生。