Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the implications of having an "implicit declaration of function" warning in C?

As the question states, what exactly are the implications of having the 'implicit declaration of function' warning? We just cranked up the warning flags on gcc and found quite a few instances of these warnings and I'm curious what type of problems this may have caused prior to fixing them?

Also, why is this a warning and not an error. How is gcc even able to successfully link this executable? As you can see in the example below, the executable functions as expected.

Take the following two files for example:

file1.c

#include <stdio.h>

int main(void)
{
   funcA();
   return 0;
}

file2.c

#include <stdio.h>

void funcA(void)
{
   puts("hello world");
}

Compile & Output

$ gcc -Wall -Wextra -c file1.c file2.c
file1.c: In function 'main':
file1.c:3: warning: implicit declaration of function 'funcA'

$ gcc -Wall -Wextra file1.o file2.o -o test.exe
$ ./test.exe
hello world
like image 335
SiegeX Avatar asked Apr 22 '10 04:04

SiegeX


People also ask

What is implicit declaration of function warning?

Implicit declaration of functions is not allowed; every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit prototype, the compiler provides an implicit declaration.

What does implicit declaration of a function mean in C?

If a name appears in a program and is not explicitly declared, it is implicitly declared. The scope of an implicit declaration is determined as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure in which the name is used.

How do you fix implicit declaration of error?

I recommend to fix this by copy-&-pasting the function name from the declaration to wherever you call it.

What is implicit declaration of function getch?

Prior to the C99 standard, the C language permitted calls to functions with no visible declaration. Such a call would in effect create an implicit declaration of a function returning int and taking arguments of whatever (promoted) type you actually passed. Depending on this has never been a good idea.


1 Answers

If the function has a definition that matches the implicit declaration (ie. it returns int and has a fixed number of arguments, and does not have a prototype), and you always call it with the correct number and types of arguments, then there are no negative implications (other than bad, obsolete style).

ie, in your code above, it is as if the function was declared as:

int funcA();

Since this doesn't match the function definition, the call to funcA() from file1.c invokes undefined behaviour, which means that it can crash. On your architecture, with your current compiler, it obviously doesn't - but architectures and compilers change.

GCC is able to link it because the symbol representing the function entry point doesn't change when the function type changes (again... on your current architecture, with your current compiler - although this is quite common).

Properly declaring your functions is a good thing - if for no other reason than that it allows you to give your function a prototype, which means that the compiler must diagnose it if you are calling it with the wrong number or types of arguments.

like image 147
caf Avatar answered Nov 15 '22 06:11

caf