I am able to compile and run the following code with gcc 4.4.7.
File: m.c
#include <stdio.h>
int main()
{
printf("%d\n", f(1, 2, 3));
}
File: f.c
int f(int a, int b)
{
return a + b;
}
Output:
$ gcc m.c f.c && ./a.out
$ 3
When the function f()
is defined in the same file, the compiler throws an error as expected. My guess is that the compiler can not detect erroneous usage of functions between compilation units. But should not the linker be able to detect it? Does the standard specify the expected behavior?
Please note that this is different than declaring a function without any parameters, which works even inside a single file. (Why does gcc allow arguments to be passed to a function defined to be with no arguments?).
I am using gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) and GNU ld version 2.20.51.0.2-5.42.el6 20100205.