Prototyping rules/recommendations sought

Discussion in 'C Programming' started by rs, Nov 28, 2003.

  1. rs

    rs Guest

    Dear All,


    I have a question regarding proptypes for functions. What is the
    recommended practice? The way I do it is to put all my external functions
    in a header file, while protyping internal (file scope) functions at the
    start of the source file. I've seen many people (especially using gcc
    under linux) don't botehr prototyping internal functions but just declare
    them inline so to speak.

    Is there any recommendations advantages/disdvantages of the approach? It
    seems to be that there is a tradeoff here:

    1. If the prototypes are going to help me by catching incorrect parameter
    types,numbers etc.. they are worth having

    2. However its a hassle to maintain them.

    So, the question is, are most modern compilers able to catch type/number
    errors so that I can avoid prototyping internal functions? What are your
    suggestions?
    rs, Nov 28, 2003
    #1
    1. Advertising

  2. rs wrote:

    > So, the question is, are most modern compilers able to catch type/number
    > errors so that I can avoid prototyping internal functions? What are your
    > suggestions?


    If a function has no declaration, only a definition, and is used before
    it is defined, it gets used without a prototype. Some compilers may
    give a warning if the expected prototype is wrong, but they may not
    infer the prototype from the later definition. So you should prototype
    such static functions. Other static functions need no declarations.

    Of course, you might forget that you are using a static function before
    declaring it unless the compiler warns about implicitly declared
    functions (e.g. with the gcc -Wimplicit option), so you may prefer to
    declare all static functions anyway.

    --
    Hallvard
    Hallvard B Furuseth, Nov 28, 2003
    #2
    1. Advertising

  3. "rs" <> schrieb im Newsbeitrag
    news:bq7mi7$lri$...
    > Dear All,
    >
    >
    > I have a question regarding proptypes for functions. What is the
    > recommended practice? The way I do it is to put all my external functions
    > in a header file, while protyping internal (file scope) functions at the
    > start of the source file. I've seen many people (especially using gcc
    > under linux) don't botehr prototyping internal functions but just declare
    > them inline so to speak.
    >
    > Is there any recommendations advantages/disdvantages of the approach? It
    > seems to be that there is a tradeoff here:
    >
    > 1. If the prototypes are going to help me by catching incorrect parameter
    > types,numbers etc.. they are worth having
    >
    > 2. However its a hassle to maintain them.
    >
    > So, the question is, are most modern compilers able to catch type/number
    > errors so that I can avoid prototyping internal functions? What are your
    > suggestions?


    Not a suggestion (who am I to give suggestions)..
    I write the functions in a compilation unit "upside down" and put protoypes
    of only those functions which are called from a different compilation unit
    into a header:

    in somecode.c:

    int foo(void)
    {
    return 1;
    }

    double bar(double some_val)
    {
    return some_val / 1.234;
    }

    int main(void)
    {
    int int_val = foo();
    double my_dbl = bar(2.55);
    return 0;
    }

    in somecode.h i have only

    double bar(double);
    because foo() is used only in somecode.c, but bar() is called from
    someothercode.c as well.

    just my <whatever currency you like> 0.02
    Robert
    Robert Stankowic, Nov 28, 2003
    #3
  4. "Robert Stankowic" <> wrote:

    > "rs" <> schrieb:

    <snip>
    > > So, the question is, are most modern compilers able to catch type/number
    > > errors so that I can avoid prototyping internal functions? What are your
    > > suggestions?

    >
    > Not a suggestion (who am I to give suggestions)..
    > I write the functions in a compilation unit "upside down" and put protoypes
    > of only those functions which are called from a different compilation unit
    > into a header:
    >
    > in somecode.c:
    >
    > int foo(void)
    > {
    > return 1;
    > }


    You may want to change this to:

    static int foo(void)
    ...

    <snip>
    > just my <whatever currency you like> 0.02


    I added mine, that makes 0.04. ;-)

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Nov 28, 2003
    #4
  5. rs wrote:

    > Dear All,
    >

    <snip>
    >
    > 1. If the prototypes are going to help me by catching incorrect parameter
    > types,numbers etc.. they are worth having

    <snip>
    > So, the question is, are most modern compilers able to catch type/number
    > errors so that I can avoid prototyping internal functions? What are your
    > suggestions?
    >


    Quoting the C90 & C99 standards: A function prototype is a declaration
    of a function that declares the types of its parameters.

    We end up with the interesting effect that the definition of a function
    is also a prototype:

    /* Both definition and prototype! */
    static int foo(char *p)
    {
    /* ... */
    return 42;
    }

    int main(void)
    {
    char *p;
    foo(p); /* Prototype in scope, full type checking */
    return 0;
    }

    Many experienced programmers do this whenever possible for static
    functions. As you noted, why maintain function prototypes for static
    functions when you don't have to?


    Mark F. Haigh
    Mark F. Haigh, Nov 29, 2003
    #5
  6. "Irrwahn Grausewitz" <> schrieb im Newsbeitrag
    news:...
    > "Robert Stankowic" <> wrote:
    >
    > > "rs" <> schrieb:

    > <snip>
    > > > So, the question is, are most modern compilers able to catch

    type/number
    > > > errors so that I can avoid prototyping internal functions? What are

    your
    > > > suggestions?

    > >
    > > Not a suggestion (who am I to give suggestions)..
    > > I write the functions in a compilation unit "upside down" and put

    protoypes
    > > of only those functions which are called from a different compilation

    unit
    > > into a header:
    > >
    > > in somecode.c:
    > >
    > > int foo(void)
    > > {
    > > return 1;
    > > }

    >
    > You may want to change this to:
    >
    > static int foo(void)
    > ...


    Yes, thank you.

    >
    > <snip>
    > > just my <whatever currency you like> 0.02

    >
    > I added mine, that makes 0.04. ;-)


    100% in less than one day.. not so bad, really :)

    Regards
    Robert
    Robert Stankowic, Nov 29, 2003
    #6
  7. rs

    rs Guest

    Does this apply only to functions that are defined BEFORE they are used or
    also to those defined after they are used?


    "Mark F. Haigh" <> wrote in message
    news:r7Xxb.61676$...
    > rs wrote:
    >
    > > Dear All,
    > >

    > <snip>
    > >
    > > 1. If the prototypes are going to help me by catching incorrect

    parameter
    > > types,numbers etc.. they are worth having

    > <snip>
    > > So, the question is, are most modern compilers able to catch type/number
    > > errors so that I can avoid prototyping internal functions? What are

    your
    > > suggestions?
    > >

    >
    > Quoting the C90 & C99 standards: A function prototype is a declaration
    > of a function that declares the types of its parameters.
    >
    > We end up with the interesting effect that the definition of a function
    > is also a prototype:
    >
    > /* Both definition and prototype! */
    > static int foo(char *p)
    > {
    > /* ... */
    > return 42;
    > }
    >
    > int main(void)
    > {
    > char *p;
    > foo(p); /* Prototype in scope, full type checking */
    > return 0;
    > }
    >
    > Many experienced programmers do this whenever possible for static
    > functions. As you noted, why maintain function prototypes for static
    > functions when you don't have to?
    >
    >
    > Mark F. Haigh
    >
    >
    >
    rs, Nov 29, 2003
    #7
  8. rs

    CBFalconer Guest

    Robert Stankowic wrote:
    > "rs" <> schrieb im Newsbeitrag
    > >
    > > I have a question regarding proptypes for functions. What is the
    > > recommended practice? The way I do it is to put all my external
    > > functions in a header file, while protyping internal (file scope)
    > > functions at the start of the source file. I've seen many people
    > > (especially using gcc under linux) don't botehr prototyping
    > > internal functions but just declare them inline so to speak.
    > >
    > > Is there any recommendations advantages/disdvantages of the
    > > approach? It seems to be that there is a tradeoff here:
    > >
    > > 1. If the prototypes are going to help me by catching incorrect
    > > parameter types,numbers etc.. they are worth having
    > >
    > > 2. However its a hassle to maintain them.
    > >
    > > So, the question is, are most modern compilers able to catch
    > > type/number errors so that I can avoid prototyping internal
    > > functions? What are your suggestions?

    >
    > Not a suggestion (who am I to give suggestions)..
    > I write the functions in a compilation unit "upside down" and put
    > protoypes of only those functions which are called from a different
    > compilation unit into a header:
    >
    > in somecode.c:
    >
    > int foo(void)
    > {
    > return 1;
    > }
    >
    > double bar(double some_val)
    > {
    > return some_val / 1.234;
    > }
    >
    > int main(void)
    > {
    > int int_val = foo();
    > double my_dbl = bar(2.55);
    > return 0;
    > }
    >
    > in somecode.h i have only
    >
    > double bar(double);
    > because foo() is used only in somecode.c, but bar() is called from
    > someothercode.c as well.


    In this case you should also declare foo() as being static, to
    avoid polluting the external name space. The general rule is that
    the header contains only things meant to be visible to other
    modules. Because of the context sensitive meaning of static, you
    should use it for all declarations visible over file scope that
    are NOT intended to be visible externally.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 29, 2003
    #8
  9. "rs" <> wrote:

    [Please don't top-post; fixed.]

    > "Mark F. Haigh" <> wrote:

    <snip>
    > > Quoting the C90 & C99 standards: A function prototype is a declaration
    > > of a function that declares the types of its parameters.
    > >
    > > We end up with the interesting effect that the definition of a function
    > > is also a prototype:
    > >
    > > /* Both definition and prototype! */
    > > static int foo(char *p)
    > > {
    > > /* ... */
    > > return 42;
    > > }
    > >
    > > int main(void)
    > > {
    > > char *p;
    > > foo(p); /* Prototype in scope, full type checking */
    > > return 0;
    > > }
    > >
    > > Many experienced programmers do this whenever possible for static
    > > functions. As you noted, why maintain function prototypes for static
    > > functions when you don't have to?

    >
    > Does this apply only to functions that are defined BEFORE they are used or
    > also to those defined after they are used?


    To have a prototype in scope when a function is called, the function
    has to be either defined (as a function definition serves as prototype
    as well) or explicitly prototyped /before/ used.

    Mark's example above is of the first kind: definition before use.

    The same example, using a separate prototype:

    /* Prototype: */
    static int foo(char *);

    int main(void)
    {
    char *p;
    foo(p); /* Prototype in scope, full type checking */
    return 0;
    }

    /* Note that the definition of foo can now savely be
    moved to the end of the file: */

    static int foo(char *p)
    {
    /* ... */
    return 42;
    }

    If you omit the prototype in this example, the compiler cannot
    perform full type checking.

    HTH
    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Nov 29, 2003
    #9
  10. rs

    Mark Gordon Guest

    On Sat, 29 Nov 2003 18:31:15 +0100
    Irrwahn Grausewitz <> wrote:

    > "rs" <> wrote:
    >
    > [Please don't top-post; fixed.]
    >
    > > "Mark F. Haigh" <> wrote:

    > <snip>
    > > > Quoting the C90 & C99 standards: A function prototype is a
    > > > declaration of a function that declares the types of its
    > > > parameters.
    > > >
    > > > We end up with the interesting effect that the definition of a
    > > > function is also a prototype:
    > > >
    > > > /* Both definition and prototype! */
    > > > static int foo(char *p)
    > > > {
    > > > /* ... */
    > > > return 42;
    > > > }
    > > >
    > > > int main(void)
    > > > {
    > > > char *p;
    > > > foo(p); /* Prototype in scope, full type checking */
    > > > return 0;
    > > > }
    > > >
    > > > Many experienced programmers do this whenever possible for static
    > > > functions. As you noted, why maintain function prototypes for
    > > > static functions when you don't have to?

    > >
    > > Does this apply only to functions that are defined BEFORE they are
    > > used or also to those defined after they are used?

    >
    > To have a prototype in scope when a function is called, the function
    > has to be either defined (as a function definition serves as prototype
    > as well) or explicitly prototyped /before/ used.


    <snip>

    <mode=awkward sod>
    Not all function definitions provide prototypes.
    static int foo(*p)
    char *p
    {
    /* ... */
    return 42;
    }

    does not provide a prototype. This, of course, is a very good reason for
    never using this type of function definition.
    </mode>
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Nov 30, 2003
    #10
  11. Mark Gordon <> wrote:
    > Irrwahn Grausewitz <> wrote:

    <snip>
    > > To have a prototype in scope when a function is called, the function
    > > has to be either defined (as a function definition serves as prototype
    > > as well) or explicitly prototyped /before/ used.

    >
    > <snip>
    >
    > <mode=awkward sod>
    > Not all function definitions provide prototypes.
    > static int foo(*p)
    > char *p
    > {
    > /* ... */
    > return 42;
    > }
    >
    > does not provide a prototype. This, of course, is a very good reason for
    > never using this type of function definition.
    > </mode>


    Yes, you are right. I stopped using "old-style" function definitions
    years ago, and almost forgot it ever existed.

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Nov 30, 2003
    #11
  12. Mark Gordon wrote:
    > Not all function definitions provide prototypes.
    > static int foo(*p)
    > char *p
    > {
    > /* ... */
    > return 42;
    > }
    >
    > does not provide a prototype. This, of course, is a very good reason for
    > never using this type of function definition.


    Very good point.

    <OT>
    Some compilers have switches to warn you when this occurs. For gcc,
    it's -Wstrict-prototypes, which will produce something like:

    foo.c:13: warning: function declaration isn't a prototype
    </OT>


    Mark F. Haigh
    Mark F. Haigh, Dec 1, 2003
    #12
  13. rs

    twalker Guest

    In article <>,
    says...
    > <mode=awkward sod>
    > Not all function definitions provide prototypes.
    > static int foo(*p)
    > char *p
    > {
    > /* ... */
    > return 42;
    > }


    Not all function compile...
    twalker, Dec 1, 2003
    #13
  14. rs

    Mark Gordon Guest

    On Mon, 1 Dec 2003 16:48:47 -0000
    twalker <> wrote:

    > In article <>,
    > says...
    > > <mode=awkward sod>
    > > Not all function definitions provide prototypes.
    > > static int foo(*p)
    > > char *p
    > > {
    > > /* ... */
    > > return 42;
    > > }

    >
    > Not all function compile...


    I put in an extra * and missed a ;. It should have been

    static int foo(p)
    char *p;
    {
    /* ... */
    return 42;
    }

    That'll teach me to compile first.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Dec 1, 2003
    #14
    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. Replies:
    5
    Views:
    356
    SpaceGirl
    Jun 17, 2004
  2. Dave Allison
    Replies:
    37
    Views:
    780
    Warren W. Gay VE3WWG
    Mar 4, 2004
  3. Ludovic Brenta
    Replies:
    86
    Views:
    1,351
    Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=
    Feb 19, 2004
  4. Dave Allison

    Announcing new scripting/prototyping language

    Dave Allison, Feb 5, 2004, in forum: C Programming
    Replies:
    35
    Views:
    863
    Warren W. Gay VE3WWG
    Mar 4, 2004
  5. Ludovic Brenta
    Replies:
    77
    Views:
    1,444
    Hyman Rosen
    Feb 26, 2004
Loading...

Share This Page