是否所有的C函数都需要在头文件中声明 [英] Do all C functions need to be declared in a header file

查看:573
本文介绍了是否所有的C函数都需要在头文件中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要在头文件中声明.c文件中使用的所有函数,或者我可以在.c文件中声明并定义它吗?如果是这样,在这种情况下.c文件中的定义是否也算作声明呢?解析方法

对于编译器,它如果声明出现在 .h .c 文件中,则无关紧要,因为编译器会看到预处理的表单。

对于人类开发人员的阅读和贡献你的代码,要好得多(避免复制和粘贴相同的声明两次),以便使用任何函数的声明在一些 #include -d标题中有多个翻译单元(即 .c 文件)。



你可以在使用它之前定义一个函数。

顺便说一句,你甚至可以避免声明你正在调用的函数它默认为返回 int 用于传统目的),但这是糟糕的口味和过时的编码方式(并且在这种情况下大多数编译器可以发出警告)。


Do I need to declare all functions I use in a .c file in a header file, or can I just declare and define right there in the .c file? If so, does a definition in the .c file in this case count as the declaration also?

解决方案

For the compiler, it does not matter if a declaration occurs in a .h or a .c file, because the compiler sees the preprocessed form.

For the human developer reading and contributing to your code, it is much better (to avoid copy&pasting the same declaration twice) to put the declaration of any function used in more than one translation unit (i.e. .c file) in some #include-d header.

And you can define a function before using it.

BTW, you might even avoid declaring a function that you are calling (it defaults to returning int for legacy purposes), but this is poor taste and obsolete way of coding (and most compilers can emit a warning in that case).

这篇关于是否所有的C函数都需要在头文件中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆