用C隐函数声明 [英] Implicit function declarations in C

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

问题描述

什么是术语函数的隐式声明的意思。调用标准库函数,而不包括适当的头文件生成一个警告作为的情况下,

What is meant by the term "implicit declaration of a function". Call to standard library function without including the appropriate header file produces a warning as in case of

int main(){
  printf("How is this not an error ?");
  return 0;
}

不应该使用没有宣布它是一个错误的函数?请详细解释一下。我搜索这个网站,发现类似的问题,但无法找到一个明确的答案。大多数的答案说了一些关于包括头文件,摆脱了警告。但我想知道这是怎么不是一个错误。

Shouldn't using a function without declaring it be an error ? Please explain in detail. I searched this site and found similar questions but could not find a definitive answer. Most answers said something about including the header file to get rid of the warning. But i want to know how is this not an error.

推荐答案

它应被视为一个错误。但C是一种古老的语言,所以这只是一个警告。结果
-Werror (GCC)编译修复了这个问题。结果

It should be considered an error. But C is an ancient language, so it's only a warning.
Compiling with -Werror (gcc) fixes this problem.

当C没有找到一个声明,它假定这种隐含声明: INT F(); ,这意味着该函数可以接收任何你给它,然后返回一个整数。如果发生这种情况是非常接近(在的情况下的printf ,它是),那么事情可以工作。在某些情况下(例如函数实际上返回一个指针和指针比整数大),可能会造成真正的麻烦。

When C doesn't find a declaration, it assumes this implicit declaration: int f();, which means the function can receive whatever you give it, and returns an integer. If this happens to be close enough (and in case of printf, it is), then things can work. In some cases (e.g. the function actually returns a pointer, and pointers are larger than ints), it may cause real trouble.

请注意,这是固定在新的C类标准(C99,C11)。在这些标准中,这是一个错误。然而, GCC 并没有实现这些标准在默认情况下,所以你仍然得到警告。

Note that this was fixed in newer C standards (C99, C11). In these standards, this is an error. However, gcc doesn't implement these standards by default, so you still get the warning.

这篇关于用C隐函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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