为什么未显示在声明之前调用函数的错误? [英] Why the error of - calling the function before being declared, is not shown?

查看:117
本文介绍了为什么未显示在声明之前调用函数的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()
{
    f();
}
int f( int i, float fl)
{
    printf("function");
}




  1. 为什么上面的代码运行在'C'中成功执行并在应报告错误时打印函数,因为在声明前调用了 f()

  1. Why does the above code runs successfully in 'C' and prints function when it should report an error, as f () is being called before it is declared.

当它在'C'中成功运行时,为什么不在'C ++'中运行。在c ++中运行时,显示:错误:在此范围内未声明'f'

When it's running successfully in 'C', then why not in 'C++'. When running in c++ it's showing: error: 'f' was not declared in this scope

如果这是因为类似编译器假定未声明的函数返回int并接受未指定数量的参数,那么为什么它也为下面的函数成功运行(即,将返回类型返回 void 而不是 int

If it is because of something like the compiler assumes an undeclared function to return an int and accept an unspecified number of arguments, then why does it runs successfully for the function below too ( i.e. when returning the returning type to void instead of int ?




void f(int i,float fl)

void f ( int i, float fl)

{

    printf("function");

}


推荐答案


  1. 旧版本的C编程语言允许函数引用而无需更早的声明。语言或语言的某些方面。这就是为什么某些编译器接受您显示的源代码的原因。您的编译器可能有开关,告诉它使用更新的版本

  1. Old versions of the C programming language permitted function references without earlier declarations. As a legacy, many current compilers still support the old language or aspects of it. This is why some compilers accept the source code you have shown. Your compiler likely has switches that tell it to use a more recent version of the C programming language or to be more strict about adherence to the standard.

C ++是最近才开发的,不具有没有声明就没有的功能。

C++ was developed more recently and does not have the legacy of functions without declarations.

不同的返回类型有效,因为汇编语言恰好以相同的方式实现。对于返回void的函数,被调用例程仅执行其操作并返回。对于一个返回int的函数,被调用的例程执行其操作,将其最终结果放入特定的处理器寄存器中,然后返回。在调用例程中,当不使用返回int的函数的返回值时,调用例程仅忽略处理器寄存器中的内容。因为忽略了寄存器,所以在调用例程中,返回void的函数和返回int的函数没有区别。在所有目标平台上并非如此;具有不同返回类型的函数之间可能会有差异,尤其是当返回类型是更复杂的对象(例如结构)时。而且,如果调用函数确实使用了返回值,则返回类型会有所不同。返回void的函数将在处理器寄存器中保留一些不受控制的值,该值应该是返回值,而调用函数将使用该值并获得意外的结果。

The different return types work because the assembly language happens to be implemented the same way. For a function returning void, the called routine simply performs its operations and returns. For a function returning int, the called routine performs its operations, puts its final result in a specific processor register, and returns. In the calling routine, when the return value of a function returning int is not used, the calling routine simply ignores what is in the processor register. Because the register is ignored, there is no difference, to the calling routine, between a function returning void and a function returning int. This will not be the case on all target platforms; there can be differences between functions with different return types, especially when the return types are more complicated objects (such as structs). And, if the calling function did use the return value, the return type would make a difference. The function returning void would leave some uncontrolled value in the processor register where a return value is supposed to be, and the calling function would use that and get unexpected results.

显而易见,这都不是您应该依靠的行为。优良作法是使用指定您希望更严格地遵守该标准并希望更多警告的编译器开关。 (我希望这些是编译器的默认设置。)并且,最好的做法是编写符合标准的代码。

As should be apparent, none of this is behavior you should rely on. It is good practice to use the compiler switches that specify you would like stricter adherence to the standard and would like more warnings. (I would prefer these be the default for compilers.) And it is good practice to write code that conforms to the standard.

这篇关于为什么未显示在声明之前调用函数的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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