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.
-
And why do you think `void main` would be valid? Also C and C++ are different languages! Keep that in mind for the future and avoid spamming tags. – too honest for this site Aug 04 '16 at 13:34
-
because we/i still use it and programs run @Olaf – Min Somai Aug 04 '16 at 13:38
-
@Min Somai Some compilers especially old or such as MS VS support this declaration. Moreover Microsoft allowed to use void Main() in its C-like language C#. However uisng void as the return type of main in C++ does not satisfy the C++ Standard. – Vlad from Moscow Aug 04 '16 at 13:42
-
@Vlad from Moscow so, it is standard that tells "void main" is not supported rather than compiler itself ? – Min Somai Aug 04 '16 at 13:46
-
@MinSomai Yes. The C++ standard says that the return type shall be of type int. – Vlad from Moscow Aug 04 '16 at 14:04
-
@MinSomai: Relying on undefined behaviour is a bad idea. Exclude me from the "we" (whoever that is), I do write correct code. – too honest for this site Aug 04 '16 at 14:06
-
The title change makes it clear that OP is asking why `void main` is accepted in certain compilers, so the marked dup does not apply. Vote to reopen. – dbush Aug 04 '16 at 17:42
-
thanks @dbush you understand me and i am very happy to get so many answers as a beginner. – Min Somai Aug 05 '16 at 12:14
2 Answers
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.

- 142,714
- 15
- 209
- 331
-
But our computer science teacher still uses it. And he never told us that. We use Turbo C++. – Min Somai Aug 04 '16 at 13:26
-
2Turbo C++ will also accept standard `int main`. Point your teacher at this answer. I'll add the relevant standardese (so he can learn :) ). – Cheers and hth. - Alf Aug 04 '16 at 13:27
-
2@MinSomai Turbo C++? The 20 year old compiler? That predates any of the C++ standards AFAIK. – Borgleader Aug 04 '16 at 13:27
-
6@MinSomai If you really want to learn C++ then I suggest you geta new teacher/school. Learning C++ with turbo C++ will do you a disservice. – NathanOliver Aug 04 '16 at 13:29
-
Beginner you know, starting from zero. @Borgleader , we also use Borland. Yeah, i know "int main" is supported by TurboC++ but no-one in our class does because you have to "return o" – Min Somai Aug 04 '16 at 13:31
-
1@MinSomai You actually do not have to have `return 0;` at the end of `main`. If you leave it out the compiler will automatically do it for you(this only works for `main`). – NathanOliver Aug 04 '16 at 13:32
-
...but if TurboC++ knows that is another question. Do yourself a favor and use G++. – deviantfan Aug 04 '16 at 13:32
-
i would love anyone suggesting me/us (my colleague), any modern c++ programming language compiler for beginners.--- strictly beginner (we are learning to find simple interest and switch statement) :) – Min Somai Aug 04 '16 at 13:33
-
@MinSomai Did you read my comment? I just did suggest you a compiler. Modern, free, etc.etc.etc. and, IMHO, the best C++ compiler today. – deviantfan Aug 04 '16 at 13:34
-
-
-
-
@MinSomai Visual Studio isn't advanced. Trying to use g++ if you are on Windows will be advanced. – Simple Aug 04 '16 at 13:47
-
@Simple I'm not sure we are on the same wavelength here, but a) the topic here is a compiler abiding the C++ standard (VS? haha). b) Installing G++ on Windows is much easier than installing VS (essentially, downloading and extracting an zip archive of MinGW64) – deviantfan Aug 04 '16 at 14:50
-
@deviantfan VS might be lacking in standard conformance in certain areas (mostly in areas only experts use), but it's nowhere near as bad as using Turbo C++. I disagree that using MinGW-w64 is easier than VS. VS comes with an IDE; a beginner would have to learn command line switches and Makefiles to use g++. – Simple Aug 04 '16 at 14:53
-
@Simple `but it's nowhere near as bad as using Turbo C++` We agree on that. `command line switches` I'm not aware that the Zip file has command line switches. My comment was about the installation. (And, imho seriously using VS without knowing some of its compiler switches is something bewteen hard and impossible). `Makefiles` exist with VS too – deviantfan Aug 04 '16 at 15:42
-
-
@MinSomai: The problem is that with a modern C++ compiler, it's more difficult to do simple graphics, not to mention simple text-screen-oriented programs. I guess that's why your teacher/school lets you use old Turbo C++. If you're using very old PCs with little disk and memory, that's also a reason to use old tools, which are more light-weight. So it really depends on things, whether it's a good idea to switch to modern compiler. But if you can, i.e. if you have modern PC, do *try also* the modern compilers. That will let you use modern intro books and tutorials. And e.g. advice on SO. ;-) – Cheers and hth. - Alf Aug 05 '16 at 12:24
-
@Cheersandhth.-Alf Thank you for answer, by the way we use pc with i7 processor as admin and there are 43+ computers attached to it. – Min Somai Aug 05 '16 at 12:34
-
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.

- 205,898
- 23
- 218
- 273