另一个函数定义内部函数的定义:它是有效的吗? [英] Function definition inside another function definition: is it valid?

查看:164
本文介绍了另一个函数定义内部函数的定义:它是有效的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到这个code

#include<stdio.h>

int main()
{
    void test(void)
    {
        printf("test");
        return;
    }
printf("main");
return 0;
}

这是男女合校工作正常,并犯规给出任何警告和错误。我没有得到为什么发生?
其他函数定义内部在这里,我已经写一个函数定义,所以它是有效的吗?

this coed works fine and doesnt give any warning and error. I am not getting why this happening ? here i have written one function definition inside other function definition so is it valid?

编辑:?如果是的话那么有没有这样任何使用

为什么GCC需要添加此功能为extension..there应该是这个心不是任何使用它?!

推荐答案

函数定义嵌套依靠GCC的扩展工作。它不是由标准所描述

Nesting of function definitions relies on a GCC extension to work. It is not described by the standard.

如果你有你的code和其它编译器的工作的愿望,那么你应该使用这种技术避免。更重要的是,如果你与其他开发商合作,我predict,其中许多人会讨厌使用这种非标准的结构的。

If you have any desire for your code to work with other compilers then you should refrain from using such techniques. What's more, if you collaborate with other developers, I predict that many of them will dislike the use of such non-standard constructs.

我不是专家,但GCC我相当肯定有编译器选项来禁用扩展。这将让你得到编译器,以确保你是不是写code,不会与其他编译器编译。

I'm not expert with GCC but I'm fairly sure there are compiler options to disable extensions. This will allow you to get the compiler to make sure you are not writing code that won't compile with other compilers.

关于你提到的更新有嵌套功能用途。他们可以通过限制规模和知名度有助于封装。然而,没有不能完全解决充分没有嵌套函数,我认为解释了为什么他们是一个很少使用的GCC特殊性问题。

Regarding your update there are uses for nested functions. They can aid encapsulation by restricting scope and visibility. However, there is no problem that cannot be solved perfectly adequately without nested functions which I think explains why they are a rarely used GCC peculiarity.

这篇关于另一个函数定义内部函数的定义:它是有效的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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