Implicit function declarations in C
It should be considered an error. But C is an ancient language, so it's only a warning.
Compiling with -Werror
(gcc) fixes this problem.
When C doesn't find a declaration, it assumes this implicit declaration: int f();
, which means the function can receive whatever you give it, and returns an integer. If this happens to be close enough (and in case of printf
, it is), then things can work. In some cases (e.g. the function actually returns a pointer, and pointers are larger than ints), it may cause real trouble.
Note that this was fixed in newer C standards (C99, C11). In these standards, this is an error. However, gcc
doesn't implement these standards by default, so you still get the warning.
Implicit declarations are not valid in C.
C99 removed this feature (present in C89).
gcc
chooses to only issue a warning by default with -std=c99
but a compiler has the right to refuse to translate such a program.
To complete the picture, since -Werror
might considered too "invasive",
for gcc (and llvm) a more precise solution is to transform just this warning in an error, using the option:
-Werror=implicit-function-declaration
See Make one gcc warning an error?
Regarding general use of -Werror
: Of course, having warningless code is recommendable, but in some stage of development it might slow down the prototyping.
Because of historical reasons going back to the very first version of C, functions are assumed to have an implicit definition of int function(int arg1, int arg2, int arg3, etc)
.
Edit: no, I was wrong about int
for the arguments. Instead it passes whatever type the argument is. So it could be an int
or a double
or a char*
. Without a prototype the compiler will pass whatever size the argument is and the function being called had better use the correct argument type to receive it.
For more details look up K&R C
.