简体   繁体   English

C中的函数内部函数

[英]Function inside function in C

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': 在功能'main'中:
  • Line 8: error: static declaration of 'func' follows non-static declaration 第8行:错误:'func'的静态声明遵循非静态声明
  • Line 4: error: previous declaration of 'func' was here 第4行:错误:'func'的先前声明在这里
  • Line 3: warning: return type of 'main' is not 'int' 第3行:警告:'main'的返回类型不是'int'

I think C allows nested class because the following code is working fine: 我认为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");
}

You are using an extension of the GNU C Compiler which allows the declarations of nested functions. 您正在使用GNU C编译器的扩展,它允许嵌套函数的声明。 The error comes from the fact, that forward declarations of nested functions under GCC's extension need to be prepended with the auto keyword. 该错误来自于以下事实:GCC扩展下的嵌套函数的前向声明需要添加auto关键字。

int a=20,b=11;
int main()
{
  int a=5, b=60; 
  auto int func(); // <--------- here
  func(); // <- call it
  printf("\nI am in main-1");

  int func(){
  printf("\nI am in funct");
  return 1;
  }

printf("\nI am in main-2");
 return 0;
}

See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html for more details. 有关详细信息,请参阅http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

ANSI C doesn't allow nested function definition. ANSI C不允许嵌套函数定义。 And your main function should return int . 你的main函数应该返回int

Nested functions are not allowed in standard C/C++. 标准C / C ++中不允许嵌套函数 Simply (forward) declare the func() inside main() if you want to define it later on. 如果你想稍后定义它,只需(向前)声明main()func()

int main()
{
  int a=5, b=60, func();

printf("\nI am in main-1");

  int func();  // <---- declare inside main()

printf("\nI am in main-2");
}


int func(){    // <---- define later
  printf("\nI am in funct");
  return 1;
}

What you are taking about is a GCC specific feature , its never been a "proper" C feature (ie part of the ANSI C specification). 您正在采取的是GCC特定功能 ,它从未成为“适当的”C功能(即ANSI C规范的一部分)。

If you want to use this feature then I believe what you are after is this: 如果你想使用这个功能,那么我相信你所追求的是:

#include <stdio.h>

int a = 20, b = 11;

int main( int argc, char* argv[] )
{
    int a = 5, b = 60;
    auto int func( void );

    printf("\nI am in main-1");

    int func( void )
    {
        printf("\nI am in funct");
        return 1;
    }

    printf("\nI am in main-2");
    return func();
}

The reason why your previous code didn't work is because nested functions have no linkage: 您以前的代码不起作用的原因是嵌套函数没有链接:

A nested function always has no linkage. 嵌套函数始终没有链接。 Declaring one with extern or static is erroneous. 用extern或static声明一个是错误的。 If you need to declare the nested function before its definition, use auto (which is otherwise meaningless for function declarations). 如果需要在定义之前声明嵌套函数,请使用auto(这对函数声明来说毫无意义)。

The above sample uses the auto keyword thusly. 以上示例因此使用auto关键字。 I've also taken the liberty of fixing your main declaration :-) 我也冒昧地修复你的main声明:-)

Nested functions are a gcc-specific extension ; 嵌套函数是特定于gcc的扩展 ; they are not universally supported. 它们并非得到普遍支持。

As far as the warning about main , the standard signatures for main are 关于main的警告, main的标准签名是

int main(void)
int main(int argc, char **argv) // or equivalent

An 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 for main if your compiler documentation explicitly lists it as such. IOW, void main()只适用于有效的签名main ,如果你的编译器的文档明确地列出了它本身。

When in doubt, use one of the standard signatures above. 如有疑问,请使用上述标准签名之一。

  1. You haven't defined func before calling it. 你没有在调用之前定义func
  2. Relates back to the original line. 与原始线路相关联。
  3. You aren't returning int. 你没有返回int。

如果删除int变量声明中的func()声明,它就有效。

C++ does not allow functions to be included inside other functions. C ++不允许将函数包含在其他函数中。

Attempting to do so in VS 2010 gives: 试图在VS 2010中这样做:

'funct' : local function definitions are illegal 'funct':本地函数定义是非法的

You need to move that function and it's declaration outside of main. 您需要移动该函数并将其声明移出main。

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

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