Implicit declaration

Discussion in 'C Programming' started by Christian Christmann, Jul 31, 2006.

  1. Hi,

    in one file I've a function that is used by another
    file containing the main function:

    file1.c:
    ....
    void test( int a ) { ... }
    ....

    main.c:
    #include "file1.h"
    int main( void )
    {
    int a = 0;
    test(a);
    return 0;
    }

    When I compile this file, I get a warning:

    "warning: implicit declaration of function `test'.

    In order to strictly meet all ANSIC-C 99 standards,
    do I always need to forward function declarations
    before they are used, like

    void test( int );

    before the main function or is this warning compiler-
    specific and not enforced by the C99 standard?

    Best regards,
    Chris
    Christian Christmann, Jul 31, 2006
    #1
    1. Advertising

  2. Christian Christmann said:

    > Hi,
    >
    > in one file I've a function that is used by another
    > file containing the main function:
    >
    > file1.c:
    > ...
    > void test( int a ) { ... }
    > ...
    >
    > main.c:
    > #include "file1.h"
    > int main( void )
    > {
    > int a = 0;
    > test(a);
    > return 0;
    > }
    >
    > When I compile this file, I get a warning:
    >
    > "warning: implicit declaration of function `test'.
    >
    > In order to strictly meet all ANSIC-C 99 standards,
    > do I always need to forward function declarations
    > before they are used, like
    >
    > void test( int );


    Yes, you do. And even for C90 (which is what I use), it's a good idea to
    prototype your functions, since otherwise the compiler is forced to make
    some assumptions (in line with the Standard's mandate) - for example, it
    would have to assume when compiling main.c that test returns int. The fact
    that test actually returns void is hardly the compiler's fault.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jul 31, 2006
    #2
    1. Advertising

  3. On Mon, 31 Jul 2006 09:10:24 +0000, Richard Heathfield wrote:

    > Yes, you do. And even for C90 (which is what I use), it's a good idea to
    > prototype your functions, since otherwise the compiler is forced to make
    > some assumptions (in line with the Standard's mandate) - for example, it
    > would have to assume when compiling main.c that test returns int. The fact
    > that test actually returns void is hardly the compiler's fault.


    Is it sufficient to just forward function prototypes of function
    that come from an included file or should also functions that are used
    in the same file where they are defined should be prototype-forwarded?
    Christian Christmann, Jul 31, 2006
    #3
  4. Christian Christmann

    Ian Collins Guest

    Christian Christmann wrote:
    > On Mon, 31 Jul 2006 09:10:24 +0000, Richard Heathfield wrote:
    >
    >
    >>Yes, you do. And even for C90 (which is what I use), it's a good idea to
    >>prototype your functions, since otherwise the compiler is forced to make
    >>some assumptions (in line with the Standard's mandate) - for example, it
    >>would have to assume when compiling main.c that test returns int. The fact
    >>that test actually returns void is hardly the compiler's fault.

    >
    >
    > Is it sufficient to just forward function prototypes of function
    > that come from an included file or should also functions that are used
    > in the same file where they are defined should be prototype-forwarded?
    >

    Any function should either appear or be declared (have a prototype)
    before it is used.

    --
    Ian Collins.
    Ian Collins, Jul 31, 2006
    #4
  5. Christian Christmann said:

    > On Mon, 31 Jul 2006 09:10:24 +0000, Richard Heathfield wrote:
    >
    >> Yes, you do. And even for C90 (which is what I use), it's a good idea to
    >> prototype your functions, since otherwise the compiler is forced to make
    >> some assumptions (in line with the Standard's mandate) - for example, it
    >> would have to assume when compiling main.c that test returns int. The
    >> fact that test actually returns void is hardly the compiler's fault.

    >
    > Is it sufficient to just forward function prototypes of function
    > that come from an included file or should also functions that are used
    > in the same file where they are defined should be prototype-forwarded?


    What you need is for the compiler to see a prototype for a function before
    it sees a call to that function. But don't forget that a function
    definition includes a prototype (assuming you're writing sane C). Thus, the
    following is fine, in either C90 or C99:

    #include <stdio.h>

    double trouble(long delay, float away) /* Yes, this IS a prototype */
    {
    return delay * away;
    }

    int main(void)
    {
    printf("%f\n", trouble(42, 3.14));
    return 0;
    }

    A prototype is a function declaration that lists the types of its
    parameters. A function definition starts with a function declaration that
    (if you are writing sane C) lists the types of its parameters, so as far as
    the compiler is concerned it's a prototype.

    So if you like, you can simply list the functions in decreasing order of
    depth, which works fine if they don't call each other. But some people
    prefer to prototype all the functions explicitly at the top of the code, so
    that the ordering of the functions within the source file is no longer of
    any importance to the compiler.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jul 31, 2006
    #5
  6. Christian Christmann <> writes:
    > in one file I've a function that is used by another
    > file containing the main function:
    >
    > file1.c:
    > ...
    > void test( int a ) { ... }
    > ...
    >
    > main.c:
    > #include "file1.h"
    > int main( void )
    > {
    > int a = 0;
    > test(a);
    > return 0;
    > }
    >
    > When I compile this file, I get a warning:
    >
    > "warning: implicit declaration of function `test'.
    >
    > In order to strictly meet all ANSIC-C 99 standards,
    > do I always need to forward function declarations
    > before they are used, like
    >
    > void test( int );
    >
    > before the main function or is this warning compiler-
    > specific and not enforced by the C99 standard?


    Since your function test is defined in file1.c, it should probably be
    declared in file1.h. For example:

    file1.h:
    #ifndef H_FILE1
    #define H_FILE1

    void test(int a);

    #endif

    file1.c:
    #include "file1.h"

    void test(int a)
    {
    }

    main.c;
    #include "file1.h"

    int main(void)
    {
    test(42);
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 31, 2006
    #6
    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. Jason
    Replies:
    3
    Views:
    11,917
    Thomas Mang
    Jan 1, 2004
  2. sb
    Replies:
    10
    Views:
    4,607
    Andre Heinen
    Feb 25, 2004
  3. Andy
    Replies:
    2
    Views:
    1,600
    Matthew
    Jan 28, 2005
  4. Ali Razavi
    Replies:
    16
    Views:
    683
    Cameron Laird
    Jun 16, 2005
  5. Replies:
    2
    Views:
    6,603
    CBFalconer
    Feb 23, 2005
Loading...

Share This Page