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 returnint
.
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 ofmain
:
int main() { /* … */ }and int main(int argc, char* argv[]) { /* … */ }
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.
§ 5.1.2.2.1 ¶ 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
- with a return type of
int
andor
- with no parameters […] or
- with two parameters […] or equivalent;
- in some other implementation-defined manner.
Furthermore, § 5.1.2.2.3 ¶ 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:
Watcom C/C++. The C Library Reference for
Watcom's C compiler says that "the main
function can be
declared to return void
".
IBM VisualAge C/C++. The Language Reference for
IBM VisualAge C/C++ says that main
"can also be declared to
return void
".
Microsoft Visual C/C++. The
MSDN documentation
says that main
"can be declared as returning
void
".
Some compilers do not provide this loophole:
MetaWare High C/C++ does not list any additional ways of defining
main
.
EMX C/C++ does not do so, either.
Other compilers are in between:
The documentation for Borland C/C++ is littered with sample
programs that define a void main()
function, but it does not
explicitly list this as a legal definition of main
, so -
somewhat ironically - most of the example code in Borland's documentation is
non-conforming.
The documentation for Comeau C/C++
implies that main
may have a return type other than int
where it discusses the semantics of falling off the end
of main()
without a return
statement,
but does not explicitly specify what additional definitions of
main
it allows.
When this page was first published, Comeau C/C++
used void main()
in its examples
as well. However, Greg Comeau was shown this web page, and in
response changed the examples to use int main()
. He did not
correct the aforementioned implication, however. (Nor did he inform me of
the change. I found out only by accident.)
The documentation for Digital Mars C/C++ also
uses void main()
in its examples.
The documentation for other vendors' C compilers has not yet been investigated at the time of writing. This list may grow.