【C++笔记】C语言include引用顺序tip

在C语言中,我们常用include去引用一个头文件。比如:#include<stdio.h>。平时我们编程时很少关心头文件的引用顺序。其实,头文件的引用顺序对于程序的编译还是有一定影响的。此处记录一个小常识,以加强对该知识点的记忆。

1.如果要在文件a.h中声明一个在文件b.h中定义的变量,而不引用b.h。那么要在a.c文件中引用b.h文件,并且要先引用b.h,后引用a.h,否则汇报变量类型未声明错误,也就是常见的某行少个“;”符号。

例如:

文件a.h:

#include <stdio.h>

Test  a_test;

文件b.h:

#include<string.h>

typedef struct{

string name;

int      age;
}Test,*pTest;

扫描二维码关注公众号,回复: 2147815 查看本文章

引用a.h和b.h的.c文件:

正确引用实例:

#include<b.h>

#include<a.h>

错误引用顺序:

#include<a.h>

#include<b.h>


转载:https://blog.csdn.net/gernie/article/details/52886638

猜你喜欢

转载自blog.csdn.net/hellozex/article/details/80885266
今日推荐