static 关键字

⚠️ 待完善
本笔记尚未添加内容。


基本概念

static关键字在C语言中有两种用途,取决于其修饰的对象类型。

修饰局部变量

  • 改变变量的生命周期为整个程序运行期间
  • 限制变量的作用域在声明的函数内部
  • 变量只被初始化一次
1
2
3
4
void counter(void) {
static int count = 0;
count++;
}

修饰全局变量/函数

  • 限制变量或函数的作用域在当前文件内
  • 实现信息隐藏
1
2
static int internal_value;
static void helper_function(void);