C 中的函数内函数

发布于 2024-12-03 04:31:16 字数 673 浏览 0 评论 0原文

任何人请详细说明这些错误:-

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

陌路黄昏 2024-12-10 04:31:16

您正在使用 GNU C 编译器的扩展,它允许声明嵌套函数。该错误源于这样一个事实: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;
}

请参阅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.

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.

街角迷惘 2024-12-10 04:31:16

ANSI C 不允许嵌套函数定义。你的主函数应该返回int

ANSI C doesn't allow nested function definition. And your main function should return int.

梦里南柯 2024-12-10 04:31:16

标准 C/C++ 中不允许嵌套函数。如果您想稍后定义它,只需(向前)在 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;
}

Nested functions are not allowed in standard C/C++. Simply (forward) declare the func() inside main() if you want to define it later on.

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;
}
初与友歌 2024-12-10 04:31:16

您正在考虑的是 GCC 特定功能,它从来都不是“适当的”C 功能(即 ANSI C 规范的一部分)。

如果您想使用此功能,那么我相信您所追求的是:

#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();
}

您之前的代码不起作用的原因是因为嵌套函数没有链接:

嵌套函数始终没有链接。用 extern 或 static 声明一个是错误的。如果需要在定义之前声明嵌套函数,请使用 auto (否则对于函数声明来说没有意义)。

上面的示例因此使用了 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:

#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. If you need to declare the nested function before its definition, use auto (which is otherwise meaningless for function declarations).

The above sample uses the auto keyword thusly. I've also taken the liberty of fixing your main declaration :-)

旧话新听 2024-12-10 04:31:16

嵌套函数是特定于 gcc 的扩展;它们并未得到普遍支持。

至于关于 main 的警告,main 的标准签名是

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

实现可能提供附加签名(某些编译器允许使用第三个参数作为环境变量),但是这些附加签名必须由实施部门记录; 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 are

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.

When in doubt, use one of the standard signatures above.

メ斷腸人バ 2024-12-10 04:31:16
  1. 在调用之前您还没有定义func
  2. 与原始线路相关。
  3. 你没有返回 int。
  1. You haven't defined func before calling it.
  2. Relates back to the original line.
  3. You aren't returning int.
鹿! 2024-12-10 04:31:16

如果您删除 int 变量声明中的 func() 声明,它就会起作用。

It works if you remove the declaration of func() in the int variable declarations.

初熏 2024-12-10 04:31:16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文