Re: Can function prototypes appear anywhere?

Discussion in 'C Programming' started by Chris Dollin, Jan 19, 2005.

  1. Chris Dollin

    Chris Dollin Guest

    Grumble wrote:

    > 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;
    > }
    >
    > $ gcc-3.4.3 -c -Wall -ansi -pedantic -O mini.c
    > /* NO WARNING */
    >
    > If I understand correctly, line 2 in main() is a function prototype
    > which declares 'bar' as a function taking an unspecified number of
    > parameters, and returning a pointer to int?
    >
    > Can function prototypes really appear anywhere in the code?


    No. They are declarations, and declarations can't appear "anywhere".
    But they can appear at block start, even in C89.

    > I suppose the better way to write that code would be:
    >
    > int *bar();
    > int main(void)
    > {
    > int *foo;
    > foo = bar(0);
    > return 0;
    > }
    >
    > Would you agree?


    I would agree, but that's a constraint of style, not language.

    --
    Chris "electric hedgehog" Dollin
     
    Chris Dollin, Jan 19, 2005
    #1
    1. Advertising

  2. Chris Dollin

    Grumble Guest

    Chris Dollin wrote:

    > Grumble wrote:
    >
    >> I've come across some strange code. Here it is, stripped down:
    >>
    >> int main(void)
    >> {
    >> int *foo;
    >> int *bar();
    >> foo = bar(0);
    >> return 0;
    >> }
    >>
    >> If I understand correctly, line 2 in main() is a function prototype
    >> which declares 'bar' as a function taking an unspecified number of
    >> parameters, and returning a pointer to int?
    >>
    >> Can function prototypes really appear anywhere in the code?

    >
    > No. They are declarations, and declarations can't appear "anywhere".
    > But they can appear at block start, even in C89.
    >
    >> I suppose the better way to write that code would be:
    >>
    >> int *bar();
    >> int main(void)
    >> {
    >> int *foo;
    >> foo = bar(0);
    >> return 0;
    >> }
    >>
    >> Would you agree?

    >
    > I would agree, but that's a constraint of style, not language.


    In the first example, the function declaration only has block scope.

    Thus, the following code is incorrect:

    int main(void)
    {
    int *foo;
    int *bar(int,int);
    foo = bar(1,2);
    return 0;
    }

    int *baz(void) { return bar(3,4); }

    $ gcc-3.4.3 -c -Wall -ansi -pedantic -O1 toto2.c
    toto2.c: In function `baz':
    toto2.c:9: warning: implicit declaration of function `bar'

    Do people actually use local function declarations?

    --
    Regards, Grumble
     
    Grumble, Jan 19, 2005
    #2
    1. Advertising

  3. Chris Dollin

    Alan Balmer Guest

    On Wed, 19 Jan 2005 15:32:10 +0100, Grumble <>
    wrote:

    >Chris Dollin wrote:
    >
    >> Grumble wrote:
    >>
    >>> I've come across some strange code. Here it is, stripped down:

    >
    >Do people actually use local function declarations?


    Apparently they do, but don't ask me why. I have seen this often in a
    particular body of legacy code I'm working with. A function handling a
    file will often declare

    FILE *fp, *fopen();

    (That's before I clean them up, of course ;-)

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Jan 19, 2005
    #3
  4. In article <cslopn$hs0$>, Chris Dollin <> writes:
    > Grumble wrote:
    >
    > > I've come across some strange code. Here it is, stripped down:
    > >
    > > int main(void)
    > > {
    > > int *foo;
    > > int *bar();
    > > foo = bar(0);
    > > return 0;
    > > }
    > >
    > > If I understand correctly, line 2 in main() is a function prototype
    > > which declares 'bar' as a function taking an unspecified number of
    > > parameters, and returning a pointer to int?
    > >
    > > Can function prototypes really appear anywhere in the code?

    >
    > No. They are declarations, and declarations can't appear "anywhere".
    > But they can appear at block start, even in C89.


    Not if they're declarations of functions with static linkage. See
    ISO 9899-1990 6.5.1:

    The declaration of an identifier for a function that has block
    scope shall have no explicit storage-class specifier other than
    extern.

    (This is part of the semantics of storage-class specifiers, not a
    constraint, so it's not required to produce a diagnostic.)

    This means that you cannot prototype a static function at block
    scope.

    I agree with Chris that even for functions with external linkage,
    declarations at block scope are a poor idea; they accomplish little
    (in practice, it's rarely useful to restrict the scope of a function
    identifier), and since the same can't be done for static-linkage
    functions, you'd end up with inconsistent placement of prototypes,
    which can't help readability.

    --
    Michael Wojcik

    Viewers are bugs for famous brands.
    -- unknown subtitler, Jackie Chan's _Thunderbolt_
     
    Michael Wojcik, Jan 19, 2005
    #4
    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. Shapper
    Replies:
    1
    Views:
    392
    Scott Allen
    May 6, 2005
  2. Denis Remezov

    order of const in function prototypes

    Denis Remezov, Jun 30, 2004, in forum: C++
    Replies:
    7
    Views:
    443
    John Harrison
    Jun 30, 2004
  3. Bo Jacobsen
    Replies:
    6
    Views:
    523
    David M. Cook
    Mar 7, 2004
  4. Lawrence Kirby

    Re: Can function prototypes appear anywhere?

    Lawrence Kirby, Jan 20, 2005, in forum: C Programming
    Replies:
    2
    Views:
    353
    Lawrence Kirby
    Jan 24, 2005
  5. Syren Baran

    function prototypes and function addresses

    Syren Baran, Jan 9, 2008, in forum: C Programming
    Replies:
    6
    Views:
    320
Loading...

Share This Page