main return value

Lars Wirzenius wirzeniu at kruuna.Helsinki.FI
Mon May 13 19:19:39 AEST 1991


In article <memo.1010816 at lynx.northeastern.edu> cschmidt at lynx.northeastern.edu writes:
>What value should the standard function MAIN return?  I ran into this
>question when porting some programs from DOS to VMS.  Alternatives:
>
>1. [void main(), no exit code]
>2. [void main(), exit(EXIT_SUCCESS)]
>3. [int main(), return EXIT_SUCCESS]
>4. [int main(), return MAIN_SUCCESS]
>[EXIT_SUCCESS == 0 causes problems in VMS]

According to the standard (or rather, all of my books covering the
standard, I don't have a copy of the real thing), the only legal
prototypes for main are:

	int main(void);
	int main(int, char **);		/* note: char ** <=> char [] */

This means that void is out of the question as the return type.

Your second problem, what value to return to the operating system, isn't
really tied up with the type of main (it is possible to terminate the
program and return a value to the operating system from anywhere in the
program with the exit function). 

If it is true that the VMS C compiler defines EXIT_SUCCESS as 0, and the
operating systems interprets that as failure, then I would think that
the VMS C compiler has a bug (actually, I think that the standard says
that 0 should always indicate success, regardless of environment, I
don't have any reference book handy, however; it would mean that VAX C
should translate exit(0) to 1, and vice versa).

I use EXIT_SUCCESS and EXIT_FAILURE, and define them if they aren't
defined by the implementation (not all environments define them).  If an
implementation defines them incorrectly, I would redefine them better,
something like:

	#ifndef EXIT_SUCCESS
	#define DEFINE_EXIT_CODES
	#endif

	#ifdef DEFINE_EXIT_CODES
	#undef EXIT_SUCCESS
	#undef EXIT_FAILURE
	#define EXIT_SUCCESS	0
	#define EXIT_FAILURE	1
	#endif

In a makefile or somewhere, the macro DEFINE_EXIT_CODES would then be
defined for systems that require a redefinition of EXIT_*. I don't think
this should cause any troubles.

I prefer EXIT_SUCCESS and EXIT_FAILURE to MAIN_* (or other similar
macros of my own), since the standard ones are (supposedly) known to all
good C programmers, whereas the MAIN_* would require everyone looking
through the code to check their definition.


-- 
Lars Wirzenius  wirzenius at cc.helsinki.fi



More information about the Comp.lang.c mailing list