Why do we need to use `int main` and not `void main` in C++?

kasperasky picture kasperasky · Jan 16, 2009 · Viewed 64.4k times · Source

Why do we need to use int main and not void main in C++?

Answer

Greg Hewgill picture Greg Hewgill · Jan 16, 2009

The short answer, is because the C++ standard requires main() to return int.

As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished. Returning a value from main() provides one way for the programmer to specify this value.