extern关键字的测试

新建test_h.h文件,内容如下:

#ifndef __TEST_H__
#define __TEST_H__
char  a='a';
#endif


新建change_a.c文件,内容如下:

#include "test_h.h"
void change_a(){
 extern char a;
 a='c';
 return 0;
}


新建hello.c文件,内容如下:

#include <stdio.h>
#include "change_a.c"
#include "test_h.h"
int main(void){
 extern char a;// a is exist
 printf("hello,world,Linux programming ! %C \n",a);
 change_a();
 printf("hello,world,Linux programming ! %C \n",a);
 return 0;
}

---------------------------------------------------------------------------

测试1:

输入gcc hello.c -o hello

输入./hello

打印结果:
hello,world,Linux programming ! a
hello,world,Linux programming ! c

结论:加载stdio.h文件,加载 change_a.c文件,没有调用change_a()函数,加载test_h.h文件。此时a='a';

---进入main()函数。

---从全局变量中获取变量a

---打印出a的值。

---调用change_a()函数,从全局变量中获取a,赋值 a='c';

---打印出a的值。

--------------------------------------------------------------------------

测试2:在测试1的环境基础上,注释掉 test_h.h 中对a的定义。

 

#ifndef __TEST_H__
#define __TEST_H__
 //char  a='a';
#endif
执行 测试1 的操作:

打印结果:

/tmp/ccovgj70.o: In function 'change_a':

hello.c:(.text+0x6): undefined reference to 'a'

/tmp/ccovgj70.o: In function 'main':

hello.c:(.text+0x14): undefined reference to 'a'

hello.c:(.text+0x39): undefined reference to 'a'

collect2: error :ld returned 1 exit status

结论:change_a()函数和main()函数都没有定义变量a。

---------------------------------------------------------------------

测试3 ,在测试1环境的基础上,在 change_a.c 中再定义一个变量 a

#include "test_h.h"
char a='d';
void change_a(){
 extern char a;
 a='c';
 return 0;
}

执行 测试1 :

打印结果:

In file included from hello.c:2:0:
change_a.c:2:6: error: redefinition of ‘a’
 char a='d';
      ^
In file included from change_a.c:1:0,
                 from hello.c:2:
test_h.h:3:8: note: previous definition of ‘a’ was here
  char  a='a';
结论:重复的定义了变量a。
------------------------------------------------------------------------

测试4,在测试1的环境基础上, 更改test_h.h 文件:

#ifndef __TEST_H__
#define __TEST_H__
char  a;
#endif

执行 测试1 :

打印结果:

hello,world,Linux programming ! 
hello,world,Linux programming ! c

结论:参考 测试1 结论。

------------------------------------------------------------------------

测试5 ,在测试1的环境基础上,新建test_h.c 文件:

#include "test_h.h"
extern char a='t';

执行 测试 1:

打印结果:

hello,world,Linux programming ! 
hello,world,Linux programming ! c

结论:参考 测试1 这是hello.c没有加载test_h.c文件,所以并没有改变a的值。
----------------------------------------------------------------------

测试6 ,在测试5的环境基础上,更改hello.c文件:

#include <stdio.h>
#include "change_a.c"
#include "test_h.c"
int main(void){
 extern char a;// a is exist
 printf("hello,world,Linux programming ! %C \n",a);
 change_a();
 printf("hello,world,Linux programming ! %C \n",a);
 return 0;
}

执行测试1:

打印结果:

xiaoming@ubuntu:~/Downloads/c_test$ gcc hello.c -o hello
In file included from hello.c:3:0:
test_h.c:2:13: warning: ‘a’ initialized and declared ‘extern’ [enabled by default]
 extern char a='t';
xiaoming@ubuntu:~/Downloads/c_test$ ./hello
hello,world,Linux programming ! t 
hello,world,Linux programming ! c 

结果:编译时出现一个警告,大概是a的初始化不能使用 extern 关键字吧。但是还是初始化成功了。

-------------------------------------------------------------------

测试7,在测试6的环境基础上,更改test_h.c文件

#include "test_h.h"
#ifndef __TEST_H__
#define __TEST_H__
extern char  a='t';
#endif
执行测试1:

打印结果:

hello,world,Linux programming ! 
hello,world,Linux programming ! c

结果:使用宏作为判断,此时未对a做初始化操作。
------------------------------------------------------------------

测试8,在测试7的基础上,更改test_h.c文件

#include "test_h.h"
char  a='t';
执行 测试1 :

打印结果:

hello,world,Linux programming ! t
hello,world,Linux programming ! c

结果:参考测试6, 此时没有使用extern 关键字,没有出现警告。

-------------------------------------------------------------------

测试9,在测试8的基础上,更改change_a.c文件

#include "test_h.c"
void change_a(){
extern char a;
a='c';
return 0;
}

执行 测试1 :

打印结果:

xiaoming@ubuntu:~/Downloads/c_test$ gcc hello.c -o hello
In file included from hello.c:3:0:
test_h.c:2:6: error: redefinition of ‘a’
 char a='t';
      ^
In file included from change_a.c:1:0,
                 from hello.c:2:
test_h.c:2:6: note: previous definition of ‘a’ was here
 char a='t';
      ^
结论:重复的定义了变量a,报错。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值