简体   繁体   English

为什么要包括标准头文件?

[英]why include standard header files?

What is the reason for including standard header files in C such as stdio.h stdlib.h etc.. gcc compiler just show a warning and declaring it implicitly like 在C中包含标准头文件(例如stdio.h stdlib.h等)的原因是什么stdlib.h编译器仅显示警告并隐式声明它,例如

implicit declaration of function 'printf' 函数'printf'的隐式声明

and program executing successfully... 并且程序成功执行...
Rather than to remove the warning, is there any reason to include header files? 除了删除警告之外,没有任何理由包括头文件吗?

Well yes, of course there's a reason. 是的,当然是有原因的。

The reason is that with the headers, you get the proper declarations, otherwise you get the implicit declaration where every function is assumed to return int . 原因是使用标头可以获得正确的声明,否则获得隐式声明,其中假定每个函数都返回int Since there are many functions (for instance malloc() ) that don't return int , assuming that they do can be very harmful. 由于有许多不返回int函数(例如malloc() ),因此假设它们确实很有害。

With function declarations the compiler can actually check that arguments and return values match the usage, which is very good. 使用函数声明,编译器实际上可以检查参数和返回值是否与用法匹配,这非常好。

Also, there are of course cases where headers declare data types, enumerations and so on that you need, there's more than functions in headers. 此外,当然,在某些情况下,标头声明了您需要的数据类型,枚举等,标头中包含的功能不止于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM