I have a simple C program with two files: main.c and test.c.
main.c
#include <stdio.h>
int main(){
printf ("%f\n", test());
return 0;
}
test.c
double test(){
return 2.5;
}
I then compile the code with the command gcc main.c test.c
. When I run it, the program outputs 0.000000
. Not 2.500000
as I expected. I don't know why.
I don't have this problem when I define test()
and main()
in the same file. I also don't have this problem if I declare double test();
just above the main function in main.c
. But with that said I'm also able to declare float test();
in main.c
, in which case the compiler gives me no warnings even though the declaration does not match the definition, and the program incorrectly outputs 0.000000
as before.
I think what I don't understand is how the linking process works and what are the real scoping rules of variables and functions defined in other files.