Zach said:
I was just reading "Advanced C" and one of the example programs I
looked at had main prototyped:
int main void();
As written, this is a non-prototype declaration that is permitted only
if both 'main' and 'void' are macros #defined to be something quite
different from what they appear to be. I presume you intended
int main(void);
int main()
{
...
}
Is there *any* reason to do this or is it just a waste of time?
What do the C standards have to say about it?
I have vague memories that prior declarations main() are prohibited,
but I couldn't find any such prohibition. It's therefore a perfectly
ordinary declaration, and therefore allowed. Whether or not it's a
good idea is not the kind fo thing the standard normally discusses.
Can anyone think of a case when you would want to prototype main()?
I think it's a good idea to prototype all functions, even main(), if
only for the same of consistency. However, I think you're actually
asking a slightly different question. I think you're asking about
providing a seperate prototype declaration for main() prior to the one
that should start the definiton of main.
If your program calls main() from other translation units, it should
pre-declare it in a seperate header, which is #defined in all
translation units where main() is either defined or used. If you call
it only in the same translation unit, but prior to the definition of
main(), you must also pre-declare main(). However, in such a case, I
would prefer to avoid the need for the seperate declaration by making
the defintion of main() itselt a prototype, prior to the first call to
main(). In all other cases, a prior declaration is pointless.
However, I doubt that this is the reason for the code you've seen. I
suspect that it's meant to shut up a warning from a compiler.
Many good compilers will give you a warning about defining a non-
static function without already having a previous declaration in
scope. This is a good thing, in general; if a function is defined or
used in two or more tranlsation units, a declaration of that function
should be put into a header file and shared between all of those
translation units using #Include. This will give you a warning message
in the event that there's any discrepancy between the declaration and
either the definition of the function or the uses of the function.
If a function will only be used in one translation unit, it should be
declared static. In some cases this will enable some better
optimiizations; in fact, it's been argued that 'inline' is redundant,
because static is sufficient to enable the same optimizations allowed
by 'inline', in almost every case where such optimizations are
desireable. However, static also enables the compiler to warn you if
you make no use of the function anywhere in the same translation unit
where it was defined; for static functions, any such function
definition is pointless, and probably an error.
main() is an exception to the above rules. However, I believe that
some of the compilers that provide this warning don't make an
exception for main().