为什么我可以在不声明C的情况下在C中调用函数,而在C ++中却不能呢? [英] Why can I call a function in C without declaring it but not in C++?

查看:49
本文介绍了为什么我可以在不声明C的情况下在C中调用函数,而在C ++中却不能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,在声明函数之前调用函数是编译器错误.但是在C语言中,它可以编译.

In C++, it is a compiler error to call a function before it is declared. But in C, it may compile.

#include<stdio.h>
int main()
{
   foo(); // foo() is called before its declaration/definition
} 

int foo()
{
   printf("Hello");
   return 0; 
} 

我已经尝试过并且知道它是正确的,但是我无法找到背后的原因.谁能解释一下编译过程实际上是如何发生的,并且两种语言都不同.

I have tried and know that it is correct but I can't get the reason behind it. Can anyone please explain how the compilation process actually takes place and differs in both the languages.

推荐答案

将代码"编译"作为程序并不意味着您可以做到.编译器应警告函数 foo()隐式声明.

The fact that the code "compiles" as a c program doesn't mean you can do it. The compiler should warn about implicit declaration of the function foo().

在这种特殊情况下,隐式声明将声明一个相同的 foo(),并且不会发生任何不良情况.

In this particular case implicit declaration would declare an identical foo() and nothing bad will happen.

但是假设以下内容是

main.c

/* Don't include any header, why would you include it if you don't
   need prototypes? */

int main(void)
{
    printf("%d\n", foo()); // Use "%d" because the compiler will
                           // implicitly declare `foo()` as
                           //
                           //              int foo()
                           //
                           // Using the "correct" specifier, would
                           // invoke undefined behavior "too".
    return 0;
}

现在假设 foo()是在另一个编译单元中定义的 1 foo.c

Now suppose foo() was defined in a different compilation unit1 foo.c as

foo.c

double foo()
{
    return 3.5;
}

它能按预期工作吗?

您可以想象如果不使用 stdio.h 而使用 malloc()会发生什么,这与我上面尝试解释的情况几乎相同.

You could imagine what would happen if you use malloc() without including stdio.h, which is pretty much the same situation I try to explain above.

因此,这样做会调用未定义的行为 2 ,因此,在这种情况下,术语" Works "不适用于可理解的意义.

So doing this will invoke undefined behavior2, thus the term "Works" is not applicable in the understandable sense in this situation.

之所以可以进行编译,是因为在很早的时候,标准,即标准.

The reason this could compile is because in the very old days it was allowed by the c standard, namely the c89 standard.

标准的问题具有从来没有允许这样做,所以您不能编译程序,如果您在调用之前调用的代码中没有原型("声明")的函数.

The c++ standard has never allowed this so you can't compile a c++ program if you call a function that has no prototype ("declaration") in the code before it's called.

现代的的问题,编译器会因为以下原因发出警告容易发生未定义行为的可能性,并且由于不难忘记添加原型或包含适当的标头,因此如果编译器可以警告此事件而不是突然发出警告,则对程序员而言更好有一个非常无法解释的错误.

Modern c compilers warn about this because of the potential for undefined behavior that can easily occur, and since it's not that hard to forget to add a prototype or to include the appropriate header it's better for the programmer if the compiler can warn about this instead of suddenly having a very unexplicable bug.

1 它不能在同一文件中编译,因为它将被定义为不同的返回类型,因为已经被隐式声明了

2 double int 是不同类型的事实开始,因此会有不确定的行为.

2Starting with the fact that double and int are different types, there will be undefined behavior because of this.

这篇关于为什么我可以在不声明C的情况下在C中调用函数,而在C ++中却不能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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