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

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

问题描述

<块引用>

如果单独编译一个函数",则不会检测到不匹配,该函数"将返回一个双精度值, main 将其视为一个 int ......根据我们所说的声明必须如何匹配定义这似乎令人惊讶.可能发生不匹配的原因是,如果没有函数原型,函数会通过它在表达式中的第一次出现来隐式声明,例如

 sum += "函数"(line);

<块引用>

如果之前没有声明的名字出现在一个表达式中并且后面跟着一个左括号,那么它被上下文声明为一个函数名,假定​​该函数返回一个int,并且不假定它的参数.

我事先为这个模棱两可的问题道歉,但这意味着什么?

顺便说一下,这是来自 Brian W. Kernighan 和 Dennis M. Ritchie 的 C 编程语言书籍第 2 版的第 73 页第 4.3 章.

解决方案

K&R2 涵盖了该语言的 1989/1990 版本.当前发布于 1999 2011 的 ISO C 标准放弃了隐式 int"规则,并要求您调用的任何函数都有可见的声明.默认情况下,编译器不一定强制执行此操作,但您应该能够请求更严格的警告——而且您绝对应该这样做.在写得很好的新代码中,规则是无关紧要的(但要理解它).

一个例子:标准的sqrt()函数在中声明:

double sqrt(double);

如果你写一个调用没有所需的#include <math.h>:

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

C90 编译器将假定 sqrt 返回 int -- 它将生成代码以将结果从 intdouble.结果将是垃圾,或者可能是崩溃.

(您可以自己手动声明sqrt,但这是错误的解决方案.)

所以不要那样做.始终包含您调用的任何函数所需的任何标头.如果未声明的函数确实返回 int(并且如果您的编译器不强制执行严格的 C99 或 C11 语义,并且满足其他一些条件),您可能会避免调用未声明的函数,但没有好处这样做的理由.

理解隐式整数"规则对于理解旧代码或写得不好的代码的行为仍然很有用,但你不应该在新代码中依赖它.

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 stringent 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 函数调用:理解“隐式 int"规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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