[英]Can we define a function into another function?
int main()
{
static int fun(){}
return 0;
}
** If we define a function into another then why this code is giving following error:** **如果我们将一个函数定义为另一个函数,那么为什么此代码会给出以下错误:**
Error: invalid storage class for function 'fun' 错误:函数“ fun”的存储类无效
C does not support nested functions, simple as that. C不支持嵌套函数,就这么简单。
To answer the question in comment about gcc non-standard nested functions and static
keyword: as explained in the gcc manual (link credit to the other answer ): 要回答注释中关于gcc非标准嵌套函数和
static
关键字的问题:如gcc手册中所述 (将信用链接到其他答案 ):
A nested function always has no linkage.
嵌套函数始终没有链接。 Declaring one with extern or static is erroneous.
用extern或static声明一个是错误的。
In other words, static
keyword does not work, because it is explicitly documented to be invalid syntax, because it can not mean what people would expect it to mean. 换句话说,
static
关键字不起作用,因为它被明确证明是无效的语法,因为它不能表示人们期望的含义。
For comparison, standard C++ has the workaround of being able to define class/struct member functions along with the class/struct also inside a function. 为了进行比较,标准C ++的变通办法是能够在函数内部定义类/结构成员函数以及类/结构。 And C++11 has proper lambda syntax too.
而且C ++ 11也具有适当的lambda语法。 But no such things in standard C.
但是标准C中没有这样的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.