Amazing: nested function definition in C

Discussion in 'C Programming' started by aladdin, Dec 13, 2005.

  1. aladdin

    aladdin Guest

    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
     
    aladdin, Dec 13, 2005
    #1
    1. Advertising

  2. aladdin

    Ben Pfaff Guest

    "aladdin" <> 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:
    web: http://benpfaff.org
     
    Ben Pfaff, Dec 13, 2005
    #2
    1. Advertising

  3. aladdin

    Artie Gold Guest

    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!"
     
    Artie Gold, Dec 13, 2005
    #3
  4. aladdin

    Guest

    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*.
     
    , Dec 13, 2005
    #4
  5. 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.
     
    Martin Ambuhl, Dec 13, 2005
    #5
  6. Artie Gold <> 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.
     
    Christopher Benson-Manica, Dec 13, 2005
    #6
  7. aladdin

    rayw Guest

    "aladdin" <> wrote in message
    news:dnlsa6$e4g$...
    > 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.
     
    rayw, Dec 13, 2005
    #7
  8. rayw <> 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.
     
    Christopher Benson-Manica, Dec 13, 2005
    #8
  9. aladdin

    Artie Gold Guest

    Christopher Benson-Manica wrote:
    > rayw <> 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!"
     
    Artie Gold, Dec 13, 2005
    #9
  10. aladdin

    Chuck F. Guest

    Christopher Benson-Manica wrote:
    > rayw <> 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/archives/2005/11/sonys_drm_rootk.html
     
    Chuck F., Dec 13, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    621
    Victor Bazarov
    Mar 13, 2005
  2. Mike Painter
    Replies:
    0
    Views:
    444
    Mike Painter
    Nov 2, 2006
  3. Ark
    Replies:
    1
    Views:
    445
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    466
    Jon Slaughter
    Oct 26, 2005
  5. Richard A. DeVenezia

    Nested function X vs. nested varX = function

    Richard A. DeVenezia, Sep 8, 2003, in forum: Javascript
    Replies:
    1
    Views:
    143
    Code Ronin
    Sep 9, 2003
Loading...

Share This Page