sa********@gmail.com wrote:
Right. It assumes a declaration for foo(), as you didn't provide one.
Naturally there isn't one defined anywhere, so the link fails.
I have an additional question.
If declaration is not needed for compilation, what are header files
for?
I thought that header files provide declarations of external functions
and data types.
Could you teach me about that?
For one thing, the compiler spontaneously makes up the function
signature and assumes that the function returns int -- and the
compiler may not guess right.
If you provide a prototype(*) then the compiler knows the parameter
types and the return type and can issue diagnostic messages if
something is wrong and it can convert the arguments in a function
call correctly to the parameter types.
Have you read the FAQ? It may have to say something which further
enlightens you.
Example, based on your own:
$ cat fooT.c
#include <stdio.h>
main()
{
printf("%d\n", foo(3));
}
$ cat foo.c
double foo (float bar)
{
return 5000.0/(1.0 + (double)bar*bar);
}
$ gcc -std=c89 -pedantic -c -o fooT.o fooT.c
$ gcc -std=c89 -pedantic -c -o foo.o foo.c
$ gcc -o foo fooT.o foo.o
$ ./foo
0
Somewhat unexpected, eh?
FWIW:
$ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c
fooT.c:4: warning: return type defaults to `int'
fooT.c: In function `main':
fooT.c:5: warning: implicit declaration of function `foo'
fooT.c:6: warning: control reaches end of non-void function
Cheers
Michael
____
(*) Note: The ... part of a variable argument list function behaves
a little bit different (in fact, it behaves quite like a function
call without prototype would behave).
--
E-Mail: Mine is an /at/ gmx /dot/ de address.