function prototype in code block

A

anoncoholic

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.
 
L

lawrence.jones

anoncoholic said:
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", 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
 
A

Andrey Tarasevich

anoncoholic said:
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.

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.
 
I

Ian Collins

Eric said:
Even so, my preference would be to declare logger() in
the same header file that defines LOGMESSAGE, and dispense
with the foolery. Maybe someone else can come up with a
more convincing use case.
One that springs to mind is proving a local wrapper for logger() by
restricting the scope of the declaration, possibly adding specific
content to the log messages.
 
A

anoncoholic

Ian said:
One that springs to mind is proving a local wrapper for logger() by
restricting the scope of the declaration, possibly adding specific
content to the log messages.

I see. Thanks for all the replies. I never knew that. It became an issue
recently with some c++ code. I was trying to declare an instance of an
object, on the stack, and call its default constructor. But the compiler
kept acting weird because it thought I was declaring a function with no
parameters that returns an object. Yep, I found the solution. Omit the
parenthesis and the default ctor will be called.
 
R

Richard

Ian Collins said:
Don't be an arse, I'm sure the OP knows the difference. The question
was perfectly OK for this group.

You have the patience of Job. But well done for not allowing "Chuck" to
continue his poison and incompetence out without reproach. He is a core
cancer in this group.
 
A

anoncoholic

Ian said:
One that springs to mind is proving a local wrapper for logger() by
restricting the scope of the declaration, possibly adding specific
content to the log messages.

I see. Thanks for all the replies. I never knew that. It became an issue
recently with some c++ code. I was trying to declare an instance of an
object, on the stack, and call its default constructor. But the compiler
kept acting weird because it thought I was declaring a function with no
parameters that returns an object. Yep, I found the solution. Omit the
parenthesis and the default ctor will be called.
 
A

anoncoholic

CBFalconer said:
You are confused. C has no constructors. This is comp.lang.c. I
suspect you really want comp.lang.c++. C++ is an entirely
different language from C.

Yes, guys... I understand. Please lower the pitch forks. ;) I think you
missed part of the thread. It was in fact a question about C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top