代码块中的函数原型 [英] function prototype in code block
问题描述
刚刚发现这是合法的(给我发消息):
int myfunc(int a)
{
int i = 0;
i = i * a;
int blah(float aa); //< - 呵呵?
返回i;
}
你可以在另一个函数中声明一个函数吗?这个
有多长时间了?注意,我不是在谈论gcc的支持
嵌套函数的扩展。只是谈论原型。
Just found out that this is legal (news to me):
int myfunc( int a )
{
int i = 0;
i = i * a;
int blah( float aa ); // <- huh?
return i;
}
You can declare a function inside another function?? How long has this
been possible? Note, i''m not talking about gcc''s extension that supports
nested functions. Just talking about the prototype.
推荐答案
anoncoholic写道:
anoncoholic wrote:
刚刚发现这是合法的(给我的消息):
int myfunc(int a)
{
int i = 0;
i = i * a;
int blah(float aa); //< - 呵呵?
返回i;
}
你可以在另一个函数中声明一个函数吗?这个
有多长时间了?注意,我不是在谈论gcc的支持
嵌套函数的扩展。只是谈论原型。
Just found out that this is legal (news to me):
int myfunc( int a )
{
int i = 0;
i = i * a;
int blah( float aa ); // <- huh?
return i;
}
You can declare a function inside another function?? How long has this
been possible? Note, i''m not talking about gcc''s extension that supports
nested functions. Just talking about the prototype.
只要我记得,就可以在
函数内声明*函数,甚至回到
标准前几十年。就个人而言,我从来没有想过它会特别出色的想法,但在某些涉及宏观扩展的情况下它可能会派上用场。
#define LOGMESSAGE(mesg)do {\
void logger(const char *,int,const char *); \
logger(__ FILE __,_ _ _ _,_,(mesg)); \\ /
}而(0)
即便如此,我的偏好是将
中的logger()声明为相同头文件,定义LOGMESSAGE,并与foolery分配
。也许其他人可以拿出一个更有说服力的用例。
-
Er ********* @ sun.com
anoncoholic写道:
anoncoholic wrote:
刚发现这是合法的(给我发消息):
int myfunc(int a)
{
int i = 0;
i = i * a;
int blah(float aa); //< - 呵呵?
返回i;
}
你可以在另一个函数中声明一个函数吗?这个
有多长时间了?注意,我不是在谈论gcc的支持
嵌套函数的扩展。只是谈论原型。
Just found out that this is legal (news to me):
int myfunc( int a )
{
int i = 0;
i = i * a;
int blah( float aa ); // <- huh?
return i;
}
You can declare a function inside another function?? How long has this
been possible? Note, i''m not talking about gcc''s extension that supports
nested functions. Just talking about the prototype.
声明(而不是定义)?这是可能的......好吧,
基本上总是如此。除了在C89 / 90中,所有声明都应该在块的最开始分组
。您的上述代码在C99中是合法的,但在C89 / 90中则不合法。
-
祝你好运,
Andrey Tarasevich
Declare (as opposed to "define")? This has been possible... well,
essentially always. Except that in C89/90 all declarations are supposed
to be grouped at the very beginning of the block. Your code above is
legal in C99, but not in C89/90 for that reason.
--
Best regards,
Andrey Tarasevich
anoncoholic< no@no.netwrote:
anoncoholic <no@no.netwrote:
刚刚发现这是合法的(给我的消息):
int myfunc(int a)
{
int i = 0;
i = i * a;
int blah(float aa); //< - 呵呵?
返回i;
}
你可以在另一个函数中声明一个函数吗?这个
有多长时间了?
Just found out that this is legal (news to me):
int myfunc( int a )
{
int i = 0;
i = i * a;
int blah( float aa ); // <- huh?
return i;
}
You can declare a function inside another function?? How long has this
been possible?
Forever,尽管在上面的例子中可执行
语句之后有声明的能力在C99中是新的。用明确的extern更清楚
,但这是函数的默认值,甚至是块内的
。
>
-Larry Jones
嘿!怎么了?你不能开个玩笑吗?!这是一个笑话! - Calvin
"Forever", although the ability to have a declaration after an executable
statement as in the above example was new in C99. It would be clearer
with an explicit "extern", but that''s the default for functions, even
inside a block.
-Larry Jones
Hey! What''s the matter? Can''t you take a joke?! It was a JOKE! -- Calvin
这篇关于代码块中的函数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!