Both programs are erroneous.
Without a prototype in scope, the compiler assumes that the function returns an int and accepts an indefinite number of parameters.
Modify your files a bit:
$ cat func.c double f(int a) { return 1.0; } $ cat main.c #include <stdio.h> int main(void) { double d = f(); printf("%lf\n", d); return 0; }
When I compile it, gcc warns me (Visual C ++ should also be in negotiation mode). But let him ignore the warning.
$ gcc -std=c99 -pedantic -W -Wall func.c main.c -o test func.c:1: warning: unused parameter 'a' main.c: In function 'main': main.c:4: warning: implicit declaration of function 'f' $ ./test 0.000000
He did not print 1, but print 0. This is because the compiler suggested that f() returns int , and the assignment d = f(); converts to int "on double . The compiler still compiled the code because it could not say that f() not defined as it was (implicitly) declared. But compilation of the above program is not required by the standard, so the compiler could reject it (e.g. using gcc -Werror !)
If we have everything in one file:
$ cat func.c >>main.c $ gcc -std=c99 -pedantic -W -Wall func.c main.c -o test main.c:4: warning: implicit declaration of function 'f' main.c: At top level: main.c:9: error: conflicting types for 'f' main.c:4: error: previous implicit declaration of 'f' was here main.c:9: warning: unused parameter 'a'
Now the compiler sees the conflict and gives an error message. But the compiler is not required to reject the above program, it may or may not be.
Most compilers do not reject the first program because they do not know if you have the correct definition of the f() function in another translation unit or not. They reject the second program because they know that you are not doing this.
Alok singhal
source share