Reputation: 161
In C89/C99/C11, in a freestanding environment, the entry point function is implementation-defined. In a hosted environment, it must be int main
in a strictly-conforming program. Most modern compilers make void main
an error. However, I see many users using void main
. Even if it is allowed in a non-conforming compiler, why would one use it? I see no practical reason why void main
would be preferred to int main
. Even in C89, it's undefined behavior to leave off the return
.
Is there a historical reason for the popularity of void main
?
I don't believe my question is primarily opinion-based. Some valid ideas have already been presented in this thread, such as Microsoft's void main
extension, and since Window's popularity, as well as it being the result of C books reprinting incorrect information. These are objective and historical reasons.
Upvotes: 16
Views: 1192
Reputation:
If you are using *NIX or MAC do following
Copy following code to test1.c
void main() {
}
Compile code in command line
cc test1.c
gcc test.c
Run following
./a.out
Run following (this will show you what is returned by your program
echo $?
Save this code to test2.c
void main() {
int i = 5;
}
Repeat 2, 3 and 4 for test2.c
That is why it is not recommended.
Why was it popular? I do not know other answers than ones above. I do have a question though; was K&R 1st edition using void main()
or int main()
?
Upvotes: 2
Reputation: 311186
Is there a historical reason for the popularity of void main?
The historical reason is, in my opinion, that books on programming in C (especially populer ones) were written before the Standard was adopted and were published after (or even before) the Standard were adopted. So many books on programming in C contained the declaration of main with return type void. Sometimes these books were republished without revising their contents. And due to the fact that old compilers usually supported the declaration of main with void such declaration were popular.
Also maybe some compiler producers (maybe even Microsoft. As you know C# allows to declare Main with void. At least Borland C++ allowed to use void main) introduced their own implementation-defined declarations of main. and main with void was a popular implementation defined declaration. So books on programming in C usually referred these popular compilers with their implementation defined main declaration.
Upvotes: 10
Reputation: 10415
Quoting Lundin's answer,
If your program is running in a hostless environment (your program is an embedded system or an operative system), it may have any return type. void main() is most common.
If your program is running in a hosted environment (on top of an OS), main() must return int, and may have additional parameters.
As the OP is asking about a hosted environment, I may quote Keith's answer,
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[]).
and Pochi's answer,
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.
From these two convincing answers, I can conclude in my humble opinion that:
At the time of C89 / C99 / C11, C might not have permitted void main
, but for small programs created by enthusiasts and learners and beginners, they might not be concerned about the return values at this stage, hence void main
became popular.
Upvotes: 6