static 和 extern使用
static :把一个变量声明为静态变量(内部)
extern : 把一个变量声明为外部变量
1)static 和 extern 对局部变量的作用
static 局部变量:延长局部变量的生命周期
void sum(){
static int a=10;
a++;
}
extern 局部变量:(不能这么用)
2)static 和 extern 对全局变量的作用
main.c 包含了 test.h
test.c static int a = 10;
表示a只能在test.c中使用,表示是一个内部的变量
main.c static int a = 15;
printf("%d\n",a); // 15
test.c int b = 100;
main.c 使用b
1) 在main.c中声明一个同名的全局变量
int b; 等价于 extern int b;
2) 在main.c中需要使用 b的之前
extern int b;
3)static 和 extern 对函数的作用
static 修饰函数:内部函数,不能在其他文件中访问
extern 修饰函数:函数定义的时候,默认就是extern的
test.c extern int sum(int a,int b){ }
main.c extern int sum(int a,int b);
sum(1,2);