为什么我可以定义另一个函数的函数吗? [英] Why can I define a function in another function?

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

问题描述

看到code以下,我在另一个函数定义一个函数,

see the code below, I define a function in another function,

void test1(void)
{
 void test2(void)
 {
   printf("test2\n");
 }
 printf("test1\n");
}

int main(void)
{
 test1();
 return 0;
}

这种用法是奇数,是C89 / C99或只是GCC的扩展的使用(我用gcc 4.6.3在Ubuntu 12编译)。我运行这个code和它输出测试2和测试1.test2只能称为test1的。

this usage is odd,is it a usage of c89/c99 or only a extension of gcc (I used gcc 4.6.3 in ubuntu 12 compiled). I run this code and it output "test2" and "test1".test2 can be only called in test1.

更重要的是,这是什么用途的普通场景或什么呢这种用法用的?

What's more,what's the common scene of this usage or what does this usage used for?

推荐答案

是的,这是一个 GCC扩展

除非你知道这不是C,它不便于携带,因此不太建议GCC将

It's not C, it's not portable, and thus not very recommended unless you know that GCC will


  • 被用来建立你的code
  • 仅有的编译器
  • 将保留在将来​​的版本中支持此功能

  • 请不要计较最小惊讶的原则。

  • Be the only compiler used to build your code
  • Will keep supporting this feature in future versions
  • Don't care about principle of least astonishment.

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

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