Amazing: nested function definition in C

A

aladdin

Hi, all

I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

My compiler is gcc (GCC) 3.3.1 (mingw special 20030804-1).


#include <stdio.h>
#include <stdlib.h>

int foo()
{
int foo1()
{
printf("in foo1\n");
}
printf("in foo\n");
foo1();
}

int main()
{
foo();
system("pause");
}

aladdin
 
B

Ben Pfaff

aladdin said:
I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

No. It's a GCC-only extension.
 
A

Artie Gold

aladdin said:
Hi, all

I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

My compiler is gcc (GCC) 3.3.1 (mingw special 20030804-1).

In GNU C, yes.
In ISO C, *no*.

<ot>
Hint: Try compiling it with the -ansi and -pedantic flags.
</ot>

HTH,
--ag
 
U

usr.root

aladdin said:
Hi, all

I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

My compiler is gcc (GCC) 3.3.1 (mingw special 20030804-1).


#include <stdio.h>
#include <stdlib.h>

int foo()
{
int foo1()
{
printf("in foo1\n");
}
printf("in foo\n");
foo1();
}

int main()
{
foo();
system("pause");
}

aladdin

it's only ok on gcc,even the g++ can not allow to do like that.just
like the first person said:
In GNU C, yes.
In ISO C, *no*.
 
M

Martin Ambuhl

aladdin said:
Hi, all

I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

My compiler is gcc (GCC) 3.3.1 (mingw special 20030804-1).

gcc without switches specifying the standard compiles a language GNUC,
which is not C but very close to it. GNUC supports nested functions, C
does not. The normal gcc implementation comes with documentation about
the horrors associated with implementing this non-standard feature.
 
C

Christopher Benson-Manica

Artie Gold said:
<ot>
Hint: Try compiling it with the -ansi and -pedantic flags.

And lest OP be unaware, -Wall (especially in light of the failure to
return values from functions).
 
R

rayw

aladdin said:
Hi, all

I found that the following code compiles successfully and works well. Does
that mean nested function definition is supported in C just like that in
Pascal?

My compiler is gcc (GCC) 3.3.1 (mingw special 20030804-1).


#include <stdio.h>
#include <stdlib.h>

int foo()
{
int foo1()
{
printf("in foo1\n");
}
printf("in foo\n");
foo1();
}

int main()
{
foo();
system("pause");
}

As you've heard - it's a nope. But, a bit of a shame that it's not in ISO
IMHO, as I think it's just a fantastic feature.
 
C

Christopher Benson-Manica

rayw said:
As you've heard - it's a nope. But, a bit of a shame that it's not in ISO
IMHO, as I think it's just a fantastic feature.

The programming world was a different place in 1989, I gather.
 
A

Artie Gold

Christopher said:
The programming world was a different place in 1989, I gather.
<OT>
It's not so much the programming world but the systems world for which C
was initially created. Though closures were certainly well known and
well appreciated in 1989 (or 1978, for that matter -- and long before)
they were not in the spirit of a language intended to form a fairly
direct abstraction over assembler. The `spirit' is more along the lines
of `You want a closure? You have the tools to build your own!'

Think also of the mini-computer world of the 'seventies when C was
initially designed. Both cycles and memory were expensive -- as remained
the case in the PC world of the 'eighties as well.

It's a design decision as opposed to being an oversight.
</OT>

HTH,
--ag (who was using closures in Algol a *long* time ago...)
 
C

Chuck F.

Christopher said:
The programming world was a different place in 1989, I gather.

Pascal, in 1968, included nested functions. However implementing
such requires some attention to both dynamic and static scope
management. C, being a close to the iron system, doesn't want to
bother with those complications, especially since other mechanisms
(such as multiple files with static declarations) can provide most
of the benefits.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top