具体内容可以参照下面这位大神(12条消息) 存储类型auto,static,extern,register的区别_firefly_2002的博客-CSDN博客
在某次笔试题中,我遇到了经常考的static问题
#include <stdio.h>
int fun(int n)
{
//局部static,只能被初始化一次,所以当第二次调用fun函数时,a不会被初始化为2
static int a=2;
a++;
return n*a;
}
int main(int argc, char *argv[])
{
int n=3;
int num=0;
//{}限定花括号中变量的作用域,使其不影响外界
{
int n=8;
fun(n);
num=fun(n);
}
//n仍然为3
num+=fun(n);
printf("%d\n",num);
return 0;
}
输出结果为36
c语言中的static的作用:
1.延长变量生命周期
2.限制作用域,//非常重要,尤其是在大型项目编程时,被static修饰的函数或者变量只能在本文件中可见,防止重名
3.被static修饰的变量只能被初始化一次,且如果没有被初始化将会被置为0.