C语言中的static关键字有以下几种作用:
限制作用域:在函数内部使用static修饰局部变量,可以将其作用域限制在函数内部,使得变量在函数执行完后仍然保持其值,且对其他函数不可见。void func() {static int count = 0;count++;printf("count: %d\n", count);}int main() {func(); // 输出: count: 1func(); // 输出: count: 2return 0;}保护数据:在C文件内使用static修饰全局变量,可以将其作用域限制在当前文件内部,使得其他文件无法访问该变量。// file1.cstatic int count = 0;// file2.cextern int count; // 编译错误,无法访问file1.c中的count变量隐藏函数:在C文件内使用static修饰函数,可以将其作用域限制在当前文件内部,使得其他文件无法调用该函数。// file1.cstatic void func() {printf("Hello, World!\n");}// file2.cextern void func(); // 编译错误,无法调用file1.c中的func函数提供持久性:在静态变量中使用static修饰,可以使得变量在程序执行过程中保持其值不变,且对其他函数不可见。int func() {static int count = 0;count++;return count;}int main() {printf("%d\n", func()); // 输出: 1printf("%d\n", func()); // 输出: 2return 0;} 
