我正在学习 C
语言,但不确定在哪里包含文件。基本上我可以在以下两种文件中完成:
选项1
test.h
int my_func(char **var);
test.c
#include <stdio.h>
#include "test.h"
int my_func(char **var) {printf("%s\n", "foo");}
int main() {...}
选项 2
test.h
#include <stdio.h>
int my_func(char **var);
test.c
#include "test.h"
int my_func(char **var) {printf("%s\n", "foo");}
int main() {...}
使用选项2,我只需要在需要该库的任何.c
文件中包含test.h
。我看到的大部分示例使用选项1。
是否有一些普遍规则来确定何时采取何种方法,还是这是个人偏好的问题?
test.h
文件是否实际声明了依赖于stdio.h
的内容,而在您的情况下并没有。但是,如果 .h 文件包含引用,例如来自 stdio.h 的EOF
或FILE*
,许多人会对使用选项1或2有不同的偏好。 - nosint my_func(FILE *output)
,那就是另一回事了。 - nos