473,661 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21082
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_Keit h) 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
43294
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
34547
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
90243
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
119565
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 I need to remove the printf call?
1
5795
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
129258
by: yeah | last post by:
hi I got this error "implicit declaration of function" what it means???
14
5311
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 << " Your Banner Here" << endl; }
3
2672
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? im on linux btw. thanks! sam
1
7570
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 have an implicit declaration?? Thanks!
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.