全局变量和静态全局变量的区别在于作用域不同
就是说一个项目里如果有多个源程序文件
非静态全局变量可以在所有源文件里调用
静态全局变量只能在本文件里调用,不允许在其他文件里调用
//file1.cpp
static int a;//只能在file1.cpp里使用
int b; //可以在file2.cpp里使用
extern int c; //file2.cpp里的c
int main(void)
{
a = 1;
b = 2;
}
//file2.cpp
static int a;//只能在file2.cpp里使用
int c; //可以在file1.cpp里使用
extern int b; //file1.cpp里的b
int fun(void)
{
c = a + b;
}
在多文件的项目开发中这样可以避免由于变量重名造成的冲突
参考资料:原创