Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Amazing: nested function definition in C (http://www.velocityreviews.com/forums/t440510-amazing-nested-function-definition-in-c.html)

aladdin 12-13-2005 07:12 AM

Amazing: nested function definition in C
 
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








Ben Pfaff 12-13-2005 07:20 AM

Re: Amazing: nested function definition in C
 
"aladdin" <buaa_aladdin@163.com> writes:

> 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.
--
Ben Pfaff
email: blp@cs.stanford.edu
web: http://benpfaff.org

Artie Gold 12-13-2005 07:20 AM

Re: Amazing: nested function definition in C
 
aladdin wrote:
> 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
>
>
> #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
>
>
>
>
>
>
>



--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"

usr.root@gmail.com 12-13-2005 07:29 AM

Re: Amazing: nested function definition in C
 

aladdin wrote:
> 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*.


Martin Ambuhl 12-13-2005 07:33 AM

Re: Amazing: nested function definition in C
 
aladdin wrote:
> 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.


Christopher Benson-Manica 12-13-2005 01:53 PM

Re: Amazing: nested function definition in C
 
Artie Gold <artiegold@austin.rr.com> wrote:

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

> </ot>


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

rayw 12-13-2005 03:45 PM

Re: Amazing: nested function definition in C
 

"aladdin" <buaa_aladdin@163.com> wrote in message
news:dnlsa6$e4g$1@news.yaako.com...
> 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.



Christopher Benson-Manica 12-13-2005 03:56 PM

Re: Amazing: nested function definition in C
 
rayw <ray.webster@gmail.com> wrote:

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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Artie Gold 12-13-2005 05:03 PM

Re: Amazing: nested function definition in C
 
Christopher Benson-Manica wrote:
> rayw <ray.webster@gmail.com> wrote:
>
>
>>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.
>

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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"

Chuck F. 12-13-2005 05:24 PM

Re: Amazing: nested function definition in C
 
Christopher Benson-Manica wrote:
> rayw <ray.webster@gmail.com> wrote:
>
>> 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.


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.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html


All times are GMT. The time now is 02:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.