会发生什么,如果我不包括头文件 [英] What will happen if I don't include header files

查看:109
本文介绍了会发生什么,如果我不包括头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运行C程序的时候,不包含头文件,会发生什么?我知道,我得到警告,但该程序运行完美。

我知道,头文件包含函数声明。因此,当我不包括他们,请问编译器数字出来?它检查所有的头文件?


解决方案

  

我知道,我得到警告,但该程序运行完美。


即$ P $对ANSI C的一个不幸的遗留:语言并不需要函数原型,所以标准C它允许这天(通常,可以制造一个警告找到函数称为无原型)

当你打电话,没有原型的函数,C编译器使得被称为关于函数的假设:


  • 函数的返回类型假定为 INT

  • 假定所有参数进行申报(即没有 ... 可变参数的东西)

  • 所有的参数都被假定为你以后默认促销传进去的,等等。

如果被调用无原型函数符合这些假设,你的程序将正常运行;否则,它是不确定的行为。

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.

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).

When you call a function with no prototype, C compiler makes assumptions about the function being called:

  • Function's return type is assumed to be int
  • All parameters are assumed to be declared (i.e. no ... vararg stuff)
  • All parameters are assumed to be whatever you pass after default promotions, and so on.

If the function being called with no prototype fits these assumptions, your program will run correctly; otherwise, it's undefined behavior.

这篇关于会发生什么,如果我不包括头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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