Christian Christmann said:
On Mon, 31 Jul 2006 09:10:24 +0000, Richard Heathfield wrote:
>Yes, you do. And even for C90 (which is what I use), it's a good idea to
prototype your functions, since otherwise the compiler is forced to make
some assumptions (in line with the Standard's mandate) - for example, it
would have to assume when compiling main.c that test returns int. The
fact that test actually returns void is hardly the compiler's fault.
Is it sufficient to just forward function prototypes of function
that come from an included file or should also functions that are used
in the same file where they are defined should be prototype-forwarded?
What you need is for the compiler to see a prototype for a function before
it sees a call to that function. But don't forget that a function
definition includes a prototype (assuming you're writing sane C). Thus, the
following is fine, in either C90 or C99:
#include <stdio.h>
double trouble(long delay, float away) /* Yes, this IS a prototype */
{
return delay * away;
}
int main(void)
{
printf("%f\n", trouble(42, 3.14));
return 0;
}
A prototype is a function declaration that lists the types of its
parameters. A function definition starts with a function declaration that
(if you are writing sane C) lists the types of its parameters, so as far as
the compiler is concerned it's a prototype.
So if you like, you can simply list the functions in decreasing order of
depth, which works fine if they don't call each other. But some people
prefer to prototype all the functions explicitly at the top of the code, so
that the ordering of the functions within the source file is no longer of
any importance to the compiler.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)