Reputation: 618
I find about int main
definition, but not of void main
in introduction of c++ programming language. I tried reading all the articles written in introduction of c++ programming language.
Upvotes: 3
Views: 1189
Reputation: 145269
void main
has never been valid in either C or C++.
C++11 §3.6.1/5:” An implementation shall not predefine the
main
function. This function shall not be overloaded. It shall have a return type of typeint
, 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[]) { /* ... */ }
…
” A
return
statement inmain
has the effect of leaving themain
function (destroying any objects with automatic storage duration) and callingstd::exit
with the return value as the argument. If control reaches the end ofmain
without encountering areturn
statement, the effect is that of executingreturn 0;
The value 0 is one value that indicates process success. The value EXIT_SUCCESS
from <stdlib.h>
may be 0 or some other value. The value EXIT_FAILURE
indicates process failure.
Upvotes: 8
Reputation: 223832
Using void main
is not standard, although there is some history regarding why some compilers allow it.
The following is from a 2008 post on programingforums.org:
In 1971, Denis Ritchie (who was working alongside Ken Thomson developing Unix) produced a "new B" by add support for the character type, and modified the early B compilers to output machine code. In 1972, "new B" was renamed to C. A preprocessor was added in 1973 or so and another programmer in the team produced the "portable I/O package", which was later renamed to the C "standard I/O routines". Hence the first version of C was born: it supported only char, integer, and pointer types. There was no void keyword.
Denis Ritchie and Brian Kernighan worked together to enhance C. Later versions of C introduced more of the standard library, such as malloc() - which originally returned pointer to char, as there was no void pointer. Hence it was always necessary in early versions of C to cast the return from malloc(). Support for floating point was also added. This became what is known as Kernighan and Ritchie (K&R) C.
In the early 1980s, a decision was made to ratify C as a standard, leading to the development of the first ANSI Standard in 1989 (then ratified as an ISO standard in 1990). In the committee process leading to the standard, a number of changes were made: in particular the void keyword was introduced, the form of function prototypes was changed.
During the standardisation process, several commercial compilers were developed. Some of these supported void main() as a work-around for compiler diagnostics about falling off the end of main(). They lobbied unsuccessfully for this to be supported by the C standard, but that was not accepted as other vendors considered it added no significant or useful new functionality. Later, in the early 1990s, when "standard compliance" became a marketing tool, they unleashed lawyers on the standard and found the wording loop-hole that - they then claimed - allows void main() to be considered as standard.
During the C standardisation process, Bjarne Stroustrup started work on the development of C++, and published the ARM (Annotated Reference Manual) with Margaret Ellis in 1990. Since that happened in parallel with the minor flurry associated with void main(), that feature was never part of C++. The ARM was the basis for development of the C++ standard, which was finally ratified by ANSI in 1998 and ISO in 1999.
During development of the 1999 C standard, there was some discussion about void main(), but it never gained traction - the push in favour was political, and overall consensus was apparently that it offered little technical benefit. Hence the 1999 C standard explicitly disallows it.
Upvotes: 6