Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Implicit declaration

Reply
Thread Tools

Implicit declaration

 
 
Christian Christmann
Guest
Posts: n/a
 
      07-31-2006
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-31-2006
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)
 
Reply With Quote
 
 
 
 
Christian Christmann
Guest
Posts: n/a
 
      07-31-2006
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?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2006
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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-31-2006
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)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2006
Christian Christmann <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implicit variable declaration and access Ali Razavi Python 16 06-16-2005 03:08 PM
Newbie: previous implicit declaration problem francescomoi@europe.com C Programming 2 02-23-2005 03:51 PM
What is an implicit extern "C" declaration? Andy C++ 2 01-28-2005 04:21 PM
implicit constructor declaration (user annoyed) sb C++ 10 02-25-2004 06:42 PM
implicit declaration of function 'int func(...)' error Jason C++ 3 01-01-2004 04:01 PM



Advertisments