extern:
extern用于访问外部变量
例子:
在config.h中
int p = 10;
char x[10] = "hello" ;
在test1.c中
#include "config.h"
#include <stdio.h>
int main(){
extern p;
p++;
extern x[];
printf("%d,%s",p,x);
return 0;}
输出:11,hello
在test2.c中
#include "config.h"
#include <stdio.h>
int main(){
extern p;
extern x[];
printf("%d,%s",p,x);
return 0;}
输出:10,hello
说明:
extern 用用访问外部变量,其定义部分只能定义一次,在定义以后不能改变
对于数组在定义时要指定数组长度,在extern声明时可以不指定其长度