i have read in various places that functions which are declared in main() cannot be called outside main. But in below program fun3() is declared inside main() and called outside main() in other functions, and IT WORKS, giving output 64.here's link http://code.geeksforgeeks.org/7EZZxQ .however,if i change fun3() return type int to void ,it fails to compile,whats reason for this behaviour?
我已经在各个地方读过,在main()中声明的函数不能在main之外调用。但是在下面的程序中,fun3()在main()中声明并在其他函数中调用main(),IT WORKS,给出输出64.here链接http://code.geeksforgeeks.org/7EZZxQ。但是,如果我改变了fun3()返回类型int为void,它无法编译,这是什么行为的原因?
#include <stdio.h>
#include <stdlib.h>
int main()
{
void fun1(int);
void fun2(int);
int fun3(int);
int num = 5;
fun1(num);
fun2(num);
}
void fun1(int no)
{
no++;
fun3(no);
}
void fun2(int no)
{
no--;
fun3(no);
}
int fun3(int n)
{
printf("%d",n);
}
there is fail in compilation when declaration of fun3() is changed.
当fun3()的声明被更改时,编译失败。
#include <stdio.h>
#include <stdlib.h>
int main()
{
void fun1(int);
void fun2(int);
void fun3(int); //fun3 return type changed to void error in compilation
int num = 5;
fun1(num);
fun2(num);
}
void fun1(int no)
{
no++;
fun3(no);
}
void fun2(int no)
{
no--;
fun3(no);
}
void fun3(int n) //fun3 return type changed to void error in compilation
{
printf("%d",n);
}
2 个解决方案
#1
2
Prior to the C99 standard, if the compiler saw a function call without a function declaration in the same scope, it assumed that the function returned int
.
在C99标准之前,如果编译器在同一范围内看到没有函数声明的函数调用,则假定函数返回int。
Neither fun1
nor fun2
declare fun3
before they call it, nor do they see the declaration in main
; the declaration of fun3
in main
is limited to the body of main
. Under C89 and earlier, the compiler will assume that the call to fun3
returns an int
. In the first example, the definition of fun3
returns an int
so the code will compile under a C89 compiler. In the second example, the definition of fun3
returns void
, which doesn't match the assumed int
type, so the code fails to compile.
fun1和fun2都没有在它们调用之前声明fun3,也没有在main中看到声明;主要的fun3声明仅限于主体。在C89及更早版本中,编译器将假定对fun3的调用返回一个int。在第一个示例中,fun3的定义返回一个int,因此代码将在C89编译器下编译。在第二个示例中,fun3的定义返回void,它与假定的int类型不匹配,因此代码无法编译。
Under C99 and later standards, neither snippet will compile; the compiler no longer assumes an implicit int
declaration for a function call. All functions must be explicitly declared or defined before they are called.
根据C99及更高版本的标准,两个片段都不会编译;编译器不再假定函数调用的隐式int声明。必须在调用之前显式声明或定义所有函数。
#2
1
Declarations are only valid in the scope they are declared in. However, in older C standards, the compiler was allowed to guess function declarations if none existed at the time of the call, which is probably what happens in your case: The compiler sees the call of fun3
in fun1
, fun2
and fun3
, and deduces (guesses) the argument types based on the argument you pass in the call.
声明仅在它们声明的范围内有效。但是,在较旧的C标准中,如果在调用时不存在,则允许编译器猜测函数声明,这可能是在您的情况下发生的情况:编译器看到在fun1,fun2和fun3中调用fun3,并根据您在调用中传递的参数推断(猜测)参数类型。
#1
2
Prior to the C99 standard, if the compiler saw a function call without a function declaration in the same scope, it assumed that the function returned int
.
在C99标准之前,如果编译器在同一范围内看到没有函数声明的函数调用,则假定函数返回int。
Neither fun1
nor fun2
declare fun3
before they call it, nor do they see the declaration in main
; the declaration of fun3
in main
is limited to the body of main
. Under C89 and earlier, the compiler will assume that the call to fun3
returns an int
. In the first example, the definition of fun3
returns an int
so the code will compile under a C89 compiler. In the second example, the definition of fun3
returns void
, which doesn't match the assumed int
type, so the code fails to compile.
fun1和fun2都没有在它们调用之前声明fun3,也没有在main中看到声明;主要的fun3声明仅限于主体。在C89及更早版本中,编译器将假定对fun3的调用返回一个int。在第一个示例中,fun3的定义返回一个int,因此代码将在C89编译器下编译。在第二个示例中,fun3的定义返回void,它与假定的int类型不匹配,因此代码无法编译。
Under C99 and later standards, neither snippet will compile; the compiler no longer assumes an implicit int
declaration for a function call. All functions must be explicitly declared or defined before they are called.
根据C99及更高版本的标准,两个片段都不会编译;编译器不再假定函数调用的隐式int声明。必须在调用之前显式声明或定义所有函数。
#2
1
Declarations are only valid in the scope they are declared in. However, in older C standards, the compiler was allowed to guess function declarations if none existed at the time of the call, which is probably what happens in your case: The compiler sees the call of fun3
in fun1
, fun2
and fun3
, and deduces (guesses) the argument types based on the argument you pass in the call.
声明仅在它们声明的范围内有效。但是,在较旧的C标准中,如果在调用时不存在,则允许编译器猜测函数声明,这可能是在您的情况下发生的情况:编译器看到在fun1,fun2和fun3中调用fun3,并根据您在调用中传递的参数推断(猜测)参数类型。