473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Implicit declaration

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
Jul 31 '06 #1
5 21048
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)
Jul 31 '06 #2
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?

Jul 31 '06 #3
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.
Jul 31 '06 #4
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)
Jul 31 '06 #5
Christian Christmann <pl*****@yahoo.dewrites:
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) ks***@mib.org <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.
Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
2
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
6
by: Johs32 | last post by:
I get this warning: warning: incompatible implicit declaration of built-in function 'printf' because I use printf in a function that I include in a .h file that is included by other files...Do...
1
by: michael | last post by:
Hi All, I have the following: #define MAX_SLEEP_TIME 1000 #include <unistd.h> void do_sleep(){ unsigned int random; while(TRUE){
6
by: yeah | last post by:
hi I got this error "implicit declaration of function" what it means???
14
by: UNCManiac37 | last post by:
Hi, I'm very new to coding, so this is a newbie question. I'm trying to print out a banner using functions instead of main: int main () { banner1 (); } void banner1 (void) { cout...
3
by: samdomville | last post by:
hello...upon compilation, i get a warning: "warning: implicit declaration of funciton funciton_name" what, techicallly, is an implicit declaration? how/where would i look to track down the error?...
1
by: samdomville | last post by:
Hello - I have a .c file that gives me a warning "implicit declaration of function function_name". However, I have #included the header file that #defines that funciton, so how is it possible to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.