以下是对头文件不宜定义变量的原因进行了详细的分析介绍,需要的朋友可以过来参考下
test-1.0使用#ifndef只是防止了头文件被重复包含(其实本例中只有一个头件,不会存在重复包含的问题),但是无法防止变量被重复定义。
# vi test.c
-------------------------------
#include
#include "test.h"
extern i;
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
int i = 10;
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include
#include "test.h"
extern char add1[];
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include
#include "test.h"
extern char add2[];
extern i;
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
以上就是头文件不宜定义变量的原因全面解析的详细内容,更多请关注0133技术站其它相关文章!