Often times I see the infamous void main()
around the forums and almost immediately a comment following the question telling the user to never use void main()
(which I am in complete agreement with). But where is the origin of void main()
?
Why am I still seeing newer people pick up the bad habit of having main
return nothing when the proper way is to return an int
.
I understand WHY this method is wrong as explained in this question and multitudes of others, but I don't how this method of declaring main
came about or even why it is still taught to some students.
The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().
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 .
void main means return the type of main function is void which means it will return nothing. void means emptyness. void main means that the functions main() does not return any value.
int main(void) represents that the function takes NO argument. Suppose, if we don't keep void in the bracket, the function will take any number of arguments. Actually, both seem to be the same but, int main(void) is technically better as it clearly mentions that main can only be called without any parameter.
Even Bjarne Stroustrup has written void main
, in C++, so it's indeed a common anti-meme, and an old one, predating Java and other contemporary languages that support void main
. Of course Bjarne has also written that void main
has never been part of either C or C++. However, for this latter statement (in his FAQ), at least as of C99 it looks as if Bjarne is wrong, because the N869 draft of the C99 standard says in its §5.1.2.2.3/1 that
“If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.”
And earlier, in its §5.1.2.2.1/1 it states about the signature of main
,
“ or in some other implementation-defined manner.”
A return type “not compatible with int
” could, for example, be void
.
So, while this is not a complete answer (I doubt that historical sources about this are available on the net), at least it goes some way towards correcting the assumptions of the question. It is not the case that void main
is a complete abomination in C and C++. But in C++ it's invalid: it's a C thing that's not supported in a hosted C++ implementation.
I have been a victim of this problem, so I think I can tell you why this happens, During our C lectures the faculties have to start our lectures using a sample program (probably "Hello World") and for that they have to use main() method.
But since they don't want to confuse students and also they don't want to get into the complexity of teaching the return types and return statements at the very start of their C programming lessons, they use(and also ask us to use) void main() and tell us to assume this as the default type till we study functions and return types in detail.
Hence this leads to develop a wrong habit of using void main() from the very first lecture of our C-Programming.
Hope that explains u well about why most of the Computer Programmers especially the newer ones pick up this bad practice.
Cheers, Mayank
Personally I think it's the following: K&R C didn't require to specify a return type and implicitly assumed it to be int and at the same time the examples in K&R didn't use a return value.
For example the first code in K&R first edition is the following:
#include <stdio.h>
main()
{
printf("Hello World\n");
}
So it's no wonder that people reading this later (after a void type was added to the language as an extension by some compilers) assumed that main actually had a void return statement.. I would've done the same thing.
Actually K&R does say later:
In the interests of simplicity, we have omitted return statements from our main functions up to this point, but we will include them hereafter, as a reminder that programs should return status to their environment.
So that's just another example of what happens when you write incorrect code and include a disclaimer later under the assumption that people will read everything before doing stupid things ;)
As one author amongst a number of others, Herbert Schildt wrote some popular but not necessarily high quality books which espoused the idea.
One egregious example is his The Annotated C Standard. He quotes the ISO/IEC 9899:1990 standard on left-hand pages and provides annotations on the right-hand pages. When he quotes section 5.1.2.2.1 Program Startup, it says:
The function called at program startup is named
main
. The implementation declares no prototype for this function. It can be defined with no parameters:int main(void) { /* ... */ }
or with two parameters (…):
int main(int argc, char *argv[]) { /* ... */ }
This doesn't include the 'or in some other implementation-defined manner' clause that was added to C99.
Then, in the annotations, he says:
Interestingly, there is no prototype for
main()
declared by the compiler. You are therefore free to declaremain()
as required by your program. For example, here are three common methods of declaringmain()
:void main(void) /* no return value, no parameters */ int main(void) /* return a value, no parameters */ /* return a value and include command-line parameters */ int main(int argc, char *argv[])
The first variation is not allowed by the C90 standard, despite what he says, but innocent readers might be confused.
Note that section 5.1.2.2.3 Program termination says:
A return from the initial call to the
main
function is equivalent to calling theexit
function with the value returned by themain
function as its argument. If themain
function executes a return that specifies no value, the termination status returned to the hosted environment is undefined.
Since you'd find that exit
takes an int
argument, it is clear from this that the return type of main
should be int
.
The commentary says:
In most implementations, the return value from
main()
, if there is one, is returned to the operating system. Remember, if you don't explicitly return a value frommain()
then the value passed to the operating system is, technically, undefined. Though most compilers will automatically return 0 when no other return value is specified (even whenmain()
is declared asvoid
), you should not rely on this fact because it is not guaranteed by the standard.
Some of this commentary is so much bovine excrement, a view in which I am not alone in holding. The only merit in the book is that it includes almost all of the C90 standard (there's one page missing from the description of fprintf
— the same page got printed twice) for far less than the cost of the standard. It's been argued that the difference in price represents the loss of value from the commentary. See Lysator generally for some information on C, and Clive Feather's review of The Annotated C Standard.
Another of his books is C: The Complete Reference, which made it to at least the 4th Edition. The 3rd Edition used void main()
extensively; this may have been cleaned up by the 4th Edition, but it's sad it took that many editions to get such a fundamental issue correct.
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