void main() is not legal in C++ but is legal in C.

You've come to this page because you've said something similar to

void main() is not legal in the C language. main() is required to return int.

This is the Frequently Given Answer to that false assertion.

The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. It has an explicit "shall" constraint upon well-formed programs. Here it is in § 3.6.1 ¶ 2:

It shall have a return type of int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

But the ISO C Standard (ISO/IEC 9899:1999) actually does not mandate this as the C++ standard does. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.

In my opinion, this is a defect in the C Standard that needs fixing with a corrigendum. (I've written a proposed revised wording that such a corrigendum could use.) It provides the authors of bad C programming books with the very loophole that they have been needing for the past decade or so.

Nonetheless, this is what the C Standard says right now, and has said for several years, and those sections of the Usenet comp.lang.c FAQ document that state that void main() is disallowed are all completely wrong. In the event of a conflict such as this between the C Standard and the Usenet comp.lang.c FAQ document, the C Standard wins.

The C Standard currently says that an implementation is free to define any additional forms for main that it cares to. § ¶ 1 of the C Standard lists the allowable forms for the definition of main. Because of the semi-colon, its final sentence parses as a follows:

It shall be defined

Furthermore, § ¶ 1 says, of main,

If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

which indicates that allowing forms that do not return int is intentional.

So if one's compiler's documentation happens to say anywhere that main may have the return type void then main may indeed have the return type void and a program with void main() is a conforming program.

This is the case for at least the following compilers:

Some compilers do not provide this loophole:

Other compilers are in between:

The documentation for other vendors' C compilers has not yet been investigated at the time of writing. This list may grow.

© Copyright 2000–2003,2010 Jonathan de Boyne Pollard. "Moral" rights asserted.
Permission is hereby granted to copy and to distribute this web page in its original, unmodified form as long as its last modification datestamp is preserved.