473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

function prototype in code block

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.
Jun 27 '08 #1
12 2241
anoncoholic wrote:
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.
It's been possible to *declare* a function inside a
function for as long as I can remember, even back to the
pre-Standard decades. Personally, I've never thought it
a particularly splendid idea, but it may come in handy
in some situations involving macro expansions:

#define LOGMESSAGE(mesg) do { \
void logger(const char*, int, const char*); \
logger(__FILE__, __LINE__, (mesg)); \
} while(0)

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.

--
Er*********@sun.com
Jun 27 '08 #2
anoncoholic wrote:
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.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #3
anoncoholic <no@no.netwrote:
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
Jun 27 '08 #4
Eric Sosman wrote:
anoncoholic wrote:
>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.

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.

--
Ian Collins.
Jun 27 '08 #5
Ian Collins wrote:
Eric Sosman wrote:
>anoncoholic wrote:
>>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.
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.
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.
Jun 27 '08 #6
anoncoholic wrote:
Ian Collins wrote:
>Eric Sosman wrote:
>>anoncoholic wrote:

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.

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.

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.
You are thoroughly confused. There are no constructors in the C
language. If you carefully examine the name of this group you will
find that it ends in c, with absolutely no ++ following.

You need to either get your compilers and languages straight, or
move to comp.lang.c++.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
CBFalconer wrote:
anoncoholic wrote:
>Ian Collins wrote:
>>Eric Sosman wrote:
anoncoholic wrote:

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

You are thoroughly confused.
Don't be an arse, I'm sure the OP knows the difference. The question
was perfectly OK for this group.

--
Ian Collins.
Jun 27 '08 #8
Ian Collins <ia******@hotmail.comwrites:
CBFalconer wrote:
>anoncoholic wrote:
>>Ian Collins wrote:
Eric Sosman wrote:
anoncoholic wrote:
>
>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.
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.
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.

You are thoroughly confused.

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.

Jun 27 '08 #9
Ian Collins wrote:
Eric Sosman wrote:
>anoncoholic wrote:
>>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.
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.
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.
Jun 27 '08 #10
anoncoholic wrote:
Ian Collins wrote:
.... snip ...
>>
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.
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #11
CBFalconer said:

<snip>
C has no constructors.
Not in the C++ sense. Nevertheless, it's quite normal to use the term
"constructor" to describe an object-building C function. Whilst I don't
habitually use foos in production code, you'll often see the following
pattern in my code:

struct foo_
{
int bar;
double baz;
};

typedef struct foo_ foo;

foo *foo_create(int bar, double baz)
{
foo *new = malloc(sizeof *new);
if(new != NULL)
{
new->bar = bar;
new->baz = baz;
}
return new;
}

I am accustomed to using the term "constructor" refer to functions that do
this kind of job.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #12
CBFalconer wrote:
anoncoholic wrote:
>Ian Collins wrote:
... snip ...
>>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.

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.
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Theese | last post by:
Consider the following declaration *inside of a function*: int j(int); My compiler (VC++ 7.1) accepts this. typeid returns a type of int __cdecl(int). Local functions are not legal in C++....
9
by: Grumble | last post by:
Hello everyone, I've come across some strange code. Here it is, stripped down: int main(void) { int *foo; int *bar(); foo = bar(0); return 0;
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
16
by: Rob Somers | last post by:
Say I have the following code: void foo(int some_int); ..... int x = 5; foo(x); ..... void foo(int some_int) {
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
5
by: kris | last post by:
Hi I have written a program which prints the hostid on a linux system. The programm uses gethostid() function call which is defined in the library file unistd.h. But my programm gets compiled...
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.