简体   繁体   English

C语言中的隐式声明

[英]Implicit declaration in C language

Consider the following quote from the C book by Dennis ritchie 请考虑Dennis ritchie的C书中的以下引用

All variables must be declared before use, although certain declarations can be made implicitly by content. 所有变量必须在使用前声明,尽管某些声明可以由内容隐式进行。

It is known that all variables of any type must be declared before using it further. 众所周知,任何类型的所有变量必须在进一步使用之前声明。 I am unaware with the latter part of the statement that certain declarations can be made implicitly by content . 我不知道声明的后半部分, 某些声明可以由内容隐含地进行

In C, in general, the variables fall under four basic data types char, int, float, double. 在C中,通常,变量属于char,int,float,double四种基本数据类型。 How can a variable from these datatypes can be used without any declaration before. 如何在没有任何声明之前使用这些数据类型的变量。 Please provide an example that shows implicit declaration based on content the variable holds. 请提供一个示例,显示基于变量保存的内容的隐式声明。

By "certain declarations" the author means declaration of things which are not variables. 通过“某些声明”,作者意味着声明不是变量的事物。 At the time the book has been written C allowed implicit declaration of functions: the compiler simply assumed that the function returns integer. 在编写本书时,C允许隐式声明函数:编译器只是假设函数返回整数。 Modern C standards make such declarations illegal. 现代C标准使这种声明非法。

When the first edition of K&R was written, there was no C standard. 当编写第一版K&R时,没有C标准。 When the second edition of K&R was written, the C89/C90 standard was about to be finalized. 在编写第二版K&R时,C89 / C90标准即将完成。 Because of the legacy from code written before C89 was finalized, the standard had to permit: 由于C89最终确定之前编写的代码遗留问题,标准必须允许:

#include <stdio.h>

double sqrt();

main(argc, argv)
    char **argv;
{
    if (argc > 1)
        printf("sqrt(%s) = %f\n", argv[1], sqrt((double)atoi(argv[1])));
    else
        printf("sqrt(%.0f) = %f\n", 2.0, sqrt(2.0));
    return 0;
}

Note that the return type of main() is implicitly int ; 请注意, main()的返回类型是隐式int ; the function argument argc is implicitly int ; 函数参数argc是隐式int ; the function atoi() has an implicit return type of int . 函数atoi()的隐式返回类型为int Note too that the argument to sqrt() had to be explicitly a double value; 另请注意, sqrt()的参数必须显式为double值; the compiler could not automatically convert the argument type because prototypes were not a part of C before the C89 standard. 编译器无法自动转换参数类型,因为原型在C89标准之前不是C的一部分。

Such code is no longer acceptable to C99 or C11 compilers. C99或C11编译器不再接受此类代码。 You could use: 你可以使用:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    if (argc > 1)
        printf("sqrt(%s) = %f\n", argv[1], sqrt(atoi(argv[1])));
    else
        printf("sqrt(%.0f) = %f\n", 2.0, sqrt(2));
    return 0;
}

This uses the standard headers to declare the functions with complete prototypes, so it is no longer necessary to cast the argument to sqrt() . 这使用标准头来声明具有完整原型的函数,因此不再需要将参数sqrt()sqrt() In C99 or C11, you could omit the return 0; 在C99或C11中,您可以省略return 0; and the effect would be the same. 效果会一样。 Personally, I don't like the loophole that allows that and continue to write the return explicitly. 就个人而言,我不喜欢允许这样做的漏洞并继续明确地写回报。 The return was necessary in C90 to send a determinate status to the environment (eg the shell the invoked the program). 在C90中返回是必要的,以向环境发送确定状态(例如,调用程序的shell)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM