19

Why has setting the entry point's return type to void in C++ always been discouraged, and was later removed by the standard and is prohibited by modern compilers? Why is it considered bad practice?

Now, as I understand C# and Java both allow the entry point's return type to be void i.e

static void main(String[] args) /* Java */
static void Main(string[] args) /* C# */

And C# and Java programmers do not consider it bad practice, they use it often in fact.

Other languages which are (only intended to be, I doubt C++ will be succeeded in this decade, at least) possible successors of C++ like the D Programming Language or Vala also allow a void main(). So as you can see, I doubt the C++ community removed it from the standard because it was too obscure or unpopular.

So my question is, Why did the C++ Community Remove void main()? What was wrong with it?

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 2
    Different language designers make different decisions. Why does it matter? – Greg Hewgill Feb 25 '12 at 07:20
  • 1
    @GregHewgill I understand that. But what were the factors & reasons that made C++ designers make the decision they did? – ApprenticeHacker Feb 25 '12 at 07:23
  • 1
    Straight from the horse's mouth :http://www2.research.att.com/~bs/bs_faq2.html#void-main :) – another.anon.coward Feb 25 '12 at 07:35
  • 2
    Actually, the question is wrong: given that a result from a program indicating at least success or failure is a Good Thing and was used in the context of where C was created (UNIX tools frequently use this indication) the question should be: why did the Java and C# communities remove the ability to indicate (and mandate) an indication of program success? This actually implicitly answers what's wrong with `void main()`: there is no result from a program. In addition `void main()` wasn't _removed_: it was never _added_. – Dietmar Kühl Feb 25 '12 at 07:57
  • 2
    I'd say using exceptions is the preferred way. If a program throws an exception that indicates a "classic" error state, then the built-in error handler should return the "classic" return-code equivalent value for you. Doing it manually is quite redundant. You still can use a function like exit(retcode) in Java, though. You can create your own top level catch block to return codes and just use exit(...). – ActiveTrayPrntrTagDataStrDrvr Mar 12 '12 at 14:40

4 Answers4

16

The C++ standards committee likely chose to require int main() because of the large body of existing code that expected to use a return statement to return a specific exit code to the runtime system. It would be unreasonable to expect all existing code to change to use exit() instead, so int main() was made a requirement in the standard.

A language such as Java, when it was designed, did not have any body of existing code that it needed to remain compatible with. Therefore, the designers could choose void main() and require the use of System.exit() for non-zero exit codes.

So, the thing that would be "wrong" with choosing void main() for the C++ standard would be that it would break existing code that expected to use return and an exit code value from main().

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    I see. So they chose `int main()` for compatibility with existing code. lol, I should have known... :D – ApprenticeHacker Feb 25 '12 at 07:33
  • 2
    I didn't attend the early C++ standards meetings but I doubt that the committee really choose anything: the use of a return value was part of C++ right from its start because it was meant to be essentially an extension of C. C essentially also used to have an `int` return (although typically implicitly declared: you could leave off `int` initially and the compiler would just assume that this is what you meant) to have a way of telling automatically if a program was successful. If the result of a program is `void` this doesn't give any indication whether it was successful. – Dietmar Kühl Feb 25 '12 at 07:52
12

C++ has never permitted void main(), though some compilers might permit it either as an extension or just because they don't diagnose it.

Similarly C has never permitted void main() other than as an extension; the same 1989 standard that introduced the void keyword defined the two standard definitions for main: int main(void) and int main(int argc, char *argv[]).

Other languages permit it because, well, they're other languages.

There is no particular advantage in being able to write void main() rather than int main(). You don't even need to explicitly return a value; falling off the end of main is equivalent to return 0; (in C++, and in C starting with C99).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • +1 for the facts. However, while it has never been permitted, even Bjarne Stroustrup himself has written `void main`, namely in the second edition (and probably also in the first, I didn't check) of "The C++ Programming Language". It's just a meme thing, like many other arbitrary and often ungood conventions. – Cheers and hth. - Alf Feb 25 '12 at 07:45
  • @Cheersandhth.-Alf: I just checked the second edition (9th printing, I think) of TC++PL, and didn't see any references to `void main()` -- though there is at least one occurrence of `main()` with an implicit `int` return type (the language no longer permits that). I checked all references to `main` from the index. Was it corrected in the edition I have, or did I miss something? – Keith Thompson Sep 28 '12 at 09:09
  • 1
    Well, like you now, Bjarne was a little skeptical about the claim that he'd done anything like that, back in 2006. See e.g. [http://groups.google.com/group/comp.lang.c++/msg/1fc435ef004677be](http://groups.google.com/group/comp.lang.c++/msg/1fc435ef004677be), where I answered "First example I found now at the end of section 7.3.2, page 233". I don't have that book available here and now, but I assume that my own ref was correct. :-) – Cheers and hth. - Alf Sep 28 '12 at 10:14
  • 1
    Son of a gun, it's still there in the 9th printing of the second edition. It's also there in the 1st printing of the third edition: section 11.4, page 275. But it's mentioned in the [errata](http://www.stroustrup.com/3rd_printing2.html), so it must have been corrected in the 2nd printing (which came out after you mentioned it on comp.lang.c++). – Keith Thompson Sep 28 '12 at 10:53
9

You generally want to know the exit status of your program. That's the reason why you have the int main() -- you return your exit status.

Pochi
  • 2,026
  • 1
  • 15
  • 11
4

It's wrong because this is not what the C++ Standard specifies as a legal main. Nobody cares about what the other languages specify. For C++ programs, only the C++ Standard is relevant, and it says int.

Puppy
  • 144,682
  • 38
  • 256
  • 465