Implicit declaration

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
R

Richard Heathfield

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.
 
C

Christian Christmann

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?
 
I

Ian Collins

Christian said:
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.
 
R

Richard Heathfield

Christian Christmann said:
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.
 
K

Keith Thompson

Christian Christmann said:
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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top