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?
In C++ the default return type of main is void, i.e. main() will not return anything. But, in C default return type of main is int, i.e. main() will return an integer value by default. In C, void main() has no defined(legit) usage, and it can sometimes throw garbage results or an error.
No. It's non-standard. The standard prototype of main is int main() with the optional command line arguments argc and argv . The int returned by main() is a way for a program to return a value to the system that invokes it.
In that case, the value returned is 0, meaning successful execution. is an error because the return type of main() is missing. It is never a good idea to use “void main()” or just “main()” as it doesn't confirm standards.
Using void main() tells the function it does not need to return anything although returning 0 is standard for the main() function so it is a matter of what is standard verses possible and Eclipse always prompts the user with what is the standard and tries to enforce standard coding while Visual Studio does not do this.
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()
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With