如果我不包含头文件会发生什么 [英] What will happen if I don't include header files
问题描述
如果我在运行 c 程序时不包含头文件会怎样?我知道我会收到警告,但程序运行良好.
What will happen if I don't include the header files when running a c program? I know that I get warnings, but the programs runs perfectly.
我知道头文件包含函数声明.因此,当我不包含它们时,编译器是如何计算出来的?它会检查所有头文件吗?
I know that the header files contain function declarations. Therefore when I don't include them, how does the compiler figure it out? Does it check all the header files?
推荐答案
我知道我收到了警告,但程序运行良好.
I know that I get warnings, but the programs runs perfectly.
这是 ANSI C 之前的不幸遗留问题:该语言不需要函数原型,因此标准 C 直到今天仍然允许它(通常会产生警告以查找没有原型调用的函数).
That is an unfortunate legacy of pre-ANSI C: the language did not require function prototypes, so the standard C allows it to this day (usually, a warning can be produced to find functions called without a prototype).
当你调用一个没有原型的函数时,C 编译器会对被调用的函数做出假设:
When you call a function with no prototype, C compiler makes assumptions about the function being called:
- 函数的返回类型假定为
int
- 假定所有参数都已声明(即没有
...
可变参数) - 假定所有参数都是您在默认促销之后传递的任何参数,依此类推.
如果在没有原型的情况下调用的函数符合这些假设,那么您的程序将正确运行;否则,这是未定义的行为.
If the function being called with no prototype fits these assumptions, your program will run correctly; otherwise, it's undefined behavior.
这篇关于如果我不包含头文件会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!