static
1.修饰局部变量
2修饰全局变量
3修饰函数
未添加static
text()
{
int a = 1;
a++;
printf("%d ", a);
}
int main()
{
int i = 0;
while (i<10)
{
text();
i++;
}
return 0;
}
运行结果
22222222222
static 修饰局部变量
text()
{
static int a = 1;
a++;
printf("%d ", a);
}
int main()
{
int i = 0;
while (i<10)
{
text();
i++;
}
return 0;
}
运行结果
2 3 4 5 6 7 8 9 10 11
当static修饰局部变量的时候,局部变量出了作用域,不销毁。
本质上说,static修饰局部变量的时候改变了变量的存储位置,将变量放在静态区。
static 修饰全局变量
【extern 声明】
static 引用只能在本文件中使用,不能跨文件使用。
static修饰函数
同全局变量,只能在本文件中使用,不能跨文件