prototype my function, please

D'Arcy J.M. Cain darcy at druid.uucp
Sat Jun 2 19:16:06 AEST 1990


In article <236 at taumet.COM> steve at taumet.UUCP (Stephen Clamage) writes:
>In article <1990May31.135230.242 at druid.uucp> darcy at druid.UUCP (D'Arcy J.M. Cain) writes:
>>So can anyone summarize the ANSI compilers (or K&R1 compilers for that
>>matter) for which decalaring main to return void causes a problem?
>
>As to "lying to the compiler" when you terminate via exit(), the
>standard says:
>	"A return from the initial call to the _main_ function function
>	is equivalent to calling the _exit_ function with the value returned
>	by the _main_ function as its argument."
>So the compiler is not allowed to be upset when exit() does not return.
>
I wasn't worried about exit() not returning.  Rather the fact that there
is no need to return from main if exit() is used to leave a program.  In
the following program:

#include <stdio.h>
int main(int argc, char **argv)
{
	printf("%s has %d arguments\n", *argv, argc);
	exit(0);
}

The compiler will give a warning that a function (main) which should
return an int does not return anything.  If, like me, you prefer super
quiet compiles you must add a return(0) statement.  You now have added
code (which won't be optimised out) which has no other purpose than to
shut the compiler up.  I will do that when necessary but try to avoid it
whenever possible.

Note that I realize that the exit above can be replaced by a return but
this is a trivial example.  In practice the exit may not even be in the
main routine.

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Government:
D'Arcy Cain Consulting             |   Organized crime with an attitude
West Hill, Ontario, Canada         |
(416) 281-6094                     |



More information about the Comp.lang.c mailing list