这些头文件有必要用C写吗?
我想要 C
中的头文件列表,这些文件不需要使用它们。
示例:
scanf(), printf(),.... etc. //can be use without stdio.h
getch()...etc. //can be used without conio.h
当我使用这些(上述)方法时,是否有必要编写这些标头(stdio.h,conio.h)?
I want a list of Header files in C
which are not necessary to use them.
Example:
scanf(), printf(),.... etc. //can be use without stdio.h
getch()...etc. //can be used without conio.h
Is is necessary to write these headers(stdio.h, conio.h) while I use these(above) methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当前标准 C99 不推荐使用没有原型的函数,并且可能会在下一版本中删除。这是有充分理由的。这种用法很容易出错并且导致难以跟踪故障。不要那样做。
Using functions without prototypes is deprecated by the current standard, C99, and will probably be removed in the next version. And this is for a very good reason. Such usage is much error prone and leads to hard to track faults. Don't do that.
在当前的 C 语言标准中,函数声明(但不是原型)是强制性的,并且原型对于像 printf 这样的可变参数函数一直是强制性的。但是,您不需要包含标头;只要您有所需的类型,您就可以自己声明/原型化函数。例如,使用
printf
,您可以这样做:但是使用
snprintf
,您至少需要stddef.h
才能获取size_t< /code>:
对于非可变参数函数,非原型声明是有效的:
In the current C language standard, function declarations (but not prototypes) are mandatory, and prototypes have always been mandatory for variadic functions like
printf
. However, you are not required to include the headers; you're free to declare/prototype the functions yourself as long as you have the required types available. For example, withprintf
, you could do:But with
snprintf
, you would need at leaststddef.h
to getsize_t
:And with non-variadic functions, a non-prototype declaration is valid:
取决于你的编译器。
对于 GCC: http://gcc.gnu.org/viewcvs/ trunk/gcc/builtins.def?view=markup
Depends on your compiler.
For GCC: http://gcc.gnu.org/viewcvs/trunk/gcc/builtins.def?view=markup
基本上你可以使用任何没有头文件的 C 函数。标头包含这些函数的原型,可以警告参数的类型或数量错误。
所以是的,您可以不使用这些标头。
但是不你不应该这样做。
但通常你不写这些头文件,这些是由构建环境提供的。
另一件事是,由于这些检查只是 C 语言中的警告,因此您应该打开这些警告并将它们视为错误。否则你将获得非常糟糕的 C 体验。
在 gcc 中,您应该始终使用选项
-W -Wall
运行,并避免这些给出的所有警告。顺便说一句,这些不是方法而是函数。
附录:由于您要将所有警告视为错误,因此您可以打开
-Werror
,这会将所有警告变为错误,从而强制执行此操作。就我个人而言,我不会使用此选项,但最终会遵守纪律清除所有警告。这使得我可以暂时忽略警告,并且通常在提交版本控制之前进行清理。
但对于团体来说,使用
-Werror
强制执行这一点当然是有意义的,例如在允许提交之前运行的测试脚本中。Basically you can use any C function without a header. The header contains the prototypes for these functions making it possible to warn about wrong type or number of parameters.
So yes you can do without these headers.
But no you shouldn't do this.
But normally you don't write these header, these are provided by the build environment.
Another thing since these checks are only warnings in C you should switch on these warnings and treat them like errors. Otherwise you are in for a very bad C experience.
In gcc you should always run with options
-W -Wall
and avoid all warnings these give.An BTW these are not methods but functions.
Addendum: since you are gonna treat all warnings as errors you might turn on
-Werror
which turns all warnings into error just enforcing this.Personally I'm not using this option but have the discipline to clean out all warnings in the end. This makes it possible to ignore the warnings for a while and usually do a clean up before I commit to version control.
But certainly for groups it makes sense to enforce this with
-Werror
e.g. in test scripts run before allowing commits.