![]() |
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 |
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 |
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!" |
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*. |
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. |
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. |
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. |
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. |
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!" |
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.