Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is void main() so popular?

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.

like image 214
user4694281 Avatar asked Mar 20 '15 14:03

user4694281


People also ask

Why do people use void main?

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

Should I put void in Main?

You shouldn't use either. The correct signature for main is (usually) int main(int argc, char* argv[]) ;-) Also, main should return int . Many compilers for embedded systems expect void main(void) .

Why don't we use void main in C?

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.

Is void main correct in C?

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.


2 Answers

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.

like image 94
Vlad from Moscow Avatar answered Sep 28 '22 06:09

Vlad from Moscow


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.

EDIT:

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.

like image 22
shauryachats Avatar answered Sep 28 '22 05:09

shauryachats