C 中的函数内函数
任何人请详细说明这些错误:-
void main()
{
int a=5, b=60, func();
printf("\nI am in main-1");
int func(){
printf("\nI am in funct");
return 1;
}
func();
printf("\nI am in main-2");
}
我得到的错误是:
- 在函数“main”中:
- 第8行:错误:“func”的静态声明遵循非静态 声明
- 第 4 行:错误:'func' 的先前声明在这里
- 第 3 行:警告:'main' 的返回类型不是 'int'
我认为 C 允许嵌套类,因为以下代码工作正常:
void outerfunc()
{
int func()
{
printf("\nI am in funct");
return 1;
}
func();
}
void main()
{
printf("\nI am in main-1");
outerfunc();
printf("\nI am in main-2");
}
Anybody please elaborate these error:-
void main()
{
int a=5, b=60, func();
printf("\nI am in main-1");
int func(){
printf("\nI am in funct");
return 1;
}
func();
printf("\nI am in main-2");
}
The errors I get are:
- In function 'main':
- Line 8: error: static declaration of 'func' follows non-static
declaration - Line 4: error: previous declaration of 'func' was here
- Line 3: warning: return type of 'main' is not 'int'
I think C allows nested class because the following code is working fine:
void outerfunc()
{
int func()
{
printf("\nI am in funct");
return 1;
}
func();
}
void main()
{
printf("\nI am in main-1");
outerfunc();
printf("\nI am in main-2");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您正在使用 GNU C 编译器的扩展,它允许声明嵌套函数。该错误源于这样一个事实:GCC 扩展下的嵌套函数的前向声明需要在前面加上
auto
关键字。请参阅http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions。 html 了解更多详细信息。
You are using an extension of the GNU C Compiler which allows the declarations of nested functions. The error comes from the fact, that forward declarations of nested functions under GCC's extension need to be prepended with the
auto
keyword.See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html for more details.
ANSI C 不允许嵌套函数定义。你的主函数应该返回int。
ANSI C doesn't allow nested function definition. And your main function should return int.
标准 C/C++ 中不允许嵌套函数。如果您想稍后定义它,只需(向前)在
main()
中声明func()
即可。Nested functions are not allowed in standard C/C++. Simply (forward) declare the
func()
insidemain()
if you want to define it later on.您正在考虑的是 GCC 特定功能,它从来都不是“适当的”C 功能(即 ANSI C 规范的一部分)。
如果您想使用此功能,那么我相信您所追求的是:
您之前的代码不起作用的原因是因为嵌套函数没有链接:
上面的示例因此使用了
auto
关键字。我还冒昧地修复了您的main
声明:-)What you are taking about is a GCC specific feature, its never been a "proper" C feature (i.e. part of the ANSI C specification).
If you want to use this feature then I believe what you are after is this:
The reason why your previous code didn't work is because nested functions have no linkage:
The above sample uses the
auto
keyword thusly. I've also taken the liberty of fixing yourmain
declaration :-)嵌套函数是特定于 gcc 的扩展;它们并未得到普遍支持。
至于关于
main
的警告,main 的标准签名是实现可能提供附加签名(某些编译器允许使用第三个参数作为环境变量),但是这些附加签名必须由实施部门记录; IOW,如果您的编译器文档明确列出了
main()
,那么它只是main
的有效签名。如有疑问,请使用上述标准签名之一。
Nested functions are a gcc-specific extension; they are not universally supported.
As far as the warning about
main
, the standard signatures for main areAn implementation may provide additional signatures (some compilers allow a third parameter for environment variables), but those additional signatures must be documented by the implementation; IOW,
void main()
is only a valid signature formain
if your compiler documentation explicitly lists it as such.When in doubt, use one of the standard signatures above.
func
。func
before calling it.如果您删除
int
变量声明中的func()
声明,它就会起作用。It works if you remove the declaration of
func()
in theint
variable declarations.C++ 不允许函数包含在其他函数中。
尝试在 VS 2010 中执行此操作会给出:
'funct' : 本地函数定义非法
您需要将该函数及其声明移到 main 之外。
C++ does not allow functions to be included inside other functions.
Attempting to do so in VS 2010 gives:
'funct' : local function definitions are illegal
You need to move that function and it's declaration outside of main.