C函数调用:了解"隐式int"规则 [英] C function calls: Understanding the "implicit int" rule

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

问题描述


  

如果功能分别编制,不匹配就不会被检测到,功能将返回两倍主会当作一个int ...在的光我们已经说过声明是如何必须匹配这个定义似乎威力惊人的。错配可能发生的原因是,如果没有函数原型,函数是隐含在离pression首次亮相宣布,如


 总和+ =函数(线);


  

如果在离pression发生时尚未pviously声明$ P $一个名字,随后是一个左括号,它是由上下文宣布为一个函数名,则假定该函数返回int ,并没有假定有关它的参数。


我事先道歉模棱两可的问题,但是这是什么意思?

顺便说一句,这是布赖恩W. Kernighan和丹尼斯·里奇先生的C语言编程的书,第2版第73页4.3章节。


解决方案

K&放大器; R2涵盖了1989/1990的语言版本。目前的ISO C标准,发表在<击> 1999年 2011年,降到了隐式int类型的规则,并且需要调用任何函数可见声明。编译器不一定在默认情况下强制执行这一点,但你应该能够要求更多stingent警告 - 你绝对应该。在精心编写新的code,规则是不相关的(但有必要去了解它)。

一个例子:标准的sqrt()功能在&LT宣布;文件math.h&GT;

 双的sqrt(双)​​;

如果你写了一个调用的没有的所需的#include&LT;&math.h中GT;

 双X = 64.0;
双Y =的sqrt(x)的;

一个C90编译​​器的假设的是开方收益 INT - 它会产生code到结果,从 INT 转换为双击。其结果将是垃圾,或者崩溃。

(您的可能的手动声明开方自己,但这是错误的解决方案。)

所以,不要做。始终包括任何头需要调用的函数。你可能会逃脱调用一个函数未声明的,如果它不会返回 INT (如果你的编译器不强制执行严格的C99或C11的语义,如果其他一些条件得到满足),但有没有很好的理由这样做。

理解隐式int类型的规则仍然是理解旧的或写得不好code的行为非常有用,但你永远不应该在新的code依赖于它。

If "a function" were compiled separately, the mismatch would not be detected, "the function" would return a double that main would treat as an int... In the light of what we have said about how declarations must match definitions this might seems surprising. The reason a mismatch can happen is that if there is no function prototype, a function is implicitly declared by its first appearance in an expression, such as

    sum += "the function"(line);

If a name that has not been previously declared occurs in an expression and is followed by a left parenthesis, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments.

I apologize beforehand for the ambiguous question, but what does this mean?

By the way this is page 73 chapter 4.3 from Brian W. Kernighan and Dennis M. Ritchie's C Programming Language book, 2nd edition.

解决方案

K&R2 covers the 1989/1990 version of the language. The current ISO C standard, published in 1999 2011, drops the "implicit int" rule, and requires a visible declaration for any function you call. Compilers don't necessarily enforce this by default, but you should be able to request more stingent warnings -- and you definitely should. In well-written new code, the rule is irrelevant (but it is necessary to understand it).

An example: the standard sqrt() function is declared in <math.h>:

double sqrt(double);

If you write a call without the required #include <math.h>:

double x = 64.0;
double y = sqrt(x);

a C90 compiler will assume that sqrt returns int -- and it will generate code to convert the result from int to double. The result will be garbage, or perhaps a crash.

(You could manually declare sqrt yourself, but that's the wrong solution.)

So don't do that. Always include whatever header is required for any function you call. You might get away with calling an undeclared function if it does return int (and if your compiler doesn't enforce strict C99 or C11 semantics, and if a few other conditions are satisfied), but there's no good reason to do so.

Understanding the "implicit int" rule is still useful for understanding the behavior of old or poorly written code, but you should never depend on it in new code.

这篇关于C函数调用:了解&QUOT;隐式int&QUOT;规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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