我还是C语言新手,有一个关于源文件和头文件的问题。我有一个像这样的头文件:
#ifndef MISC_H_
#define MISC_H_
#define BYTE 8
#include <stdbool.h>
#include <stdio.h>
#include "DataStruct.h"
bool S_areEqual(char *firstString, char *secondString); /* (1) */
bool S_randomDataStructureCheck(char *string, DataStruct *data); /* (2) */
#endif
函数参数中使用了
bool
,因此在源文件中也有。我需要在头文件和源文件中都加入#include <stdbool.h>
吗?是否存在某些情况下需要,而某些情况下不需要?如果另一个头文件中有一个用作函数参数的
typedef
,那么在头文件和源文件中都需要加入#include "DataStruct.h"
吗?
这是什么标准?
<stdio.h>
。它没有指定任何类型的引用。你确实需要<stdbool.h>
和"DataStruct.h"
。另请参见《C语言头文件使用模式的好的文档参考是什么?》和《我应该在头文件中使用#include吗?》。 - Jonathan Leffler<stdio.h>
,但它应该隐藏在源文件中,而不是包含在头文件中。头文件定义了接口。它应该是自包含的(因此需要包括<stdbool.h>
和"DataStruct.h"
);它应该是最小化的,不包含多余的头文件(所以没有<stdio.h>
)! - Jonathan Leffler