Why wasn't void main() standardized as a valid signature for the main function?
Asked Answered
O

4

20

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?

Overarch answered 25/2, 2012 at 7:16 Comment(5)
Different language designers make different decisions. Why does it matter?Afterclap
@GregHewgill I understand that. But what were the factors & reasons that made C++ designers make the decision they did?Overarch
Straight from the horse's mouth :www2.research.att.com/~bs/bs_faq2.html#void-main :)Quaggy
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.Arsonist
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(...).Hamamelidaceous
A
17

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().

Afterclap answered 25/2, 2012 at 7:30 Comment(2)
I see. So they chose int main() for compatibility with existing code. lol, I should have known... :DOverarch
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.Arsonist
J
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).

Jecho answered 25/2, 2012 at 7:24 Comment(4)
+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.Varanasi
@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?Jecho
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, 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. :-)Varanasi
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, so it must have been corrected in the 2nd printing (which came out after you mentioned it on comp.lang.c++).Jecho
A
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.

Adjectival answered 25/2, 2012 at 7:21 Comment(0)
I
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.

Intellectualism answered 25/2, 2012 at 7:21 Comment(5)
The question, in other words, is why does it say that?Indiana
@Soufiane: The question asks why it's not valid.#Intellectualism
It's wrong because this is not what the C++ Standard specifies ... only the C++ Standard is relevant . lol, from this I gather that the C++ Standard == The Newest Divine TestamentOverarch
If you're writing a C++ program, then, well, yes.Intellectualism
" For C++ programs, only the C++ Standard is relevant, and it says int. ". -1 , because clearly the C standard is equally important to C++ programmers in practise.Jeu

© 2022 - 2024 — McMap. All rights reserved.