2

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.

Quentin
  • 62,093
  • 7
  • 131
  • 191
Min Somai
  • 567
  • 6
  • 13

2 Answers2

7

void main has never been valid in either C or C++.

C++11 §3.6.1/2:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

C++11 §3.6.1/5:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 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.

Cheers and hth. - Alf
  • 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
  • 2
    Turbo 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
  • @devian you mean to use TurboG++?? never heard of that – Min Somai Aug 04 '16 at 13:36
  • @MinSomai no, just "G++" (more often capitalized "g++"). – Quentin Aug 04 '16 at 13:37
  • @MinSomai or Visual Studio on Windows. – Simple Aug 04 '16 at 13:43
  • @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
  • @deviantfan so i should use g++ as a beginner, right? – Min Somai Aug 05 '16 at 12:12
  • @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
  • `i7 processor` That's plenty to use GCC. – deviantfan Aug 05 '16 at 23:11
5

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.

dbush
  • 205,898
  • 23
  • 218
  • 273