What should we choose Int main(), Void main() & main()?

MOHAMMED CHAND PASHA
3 min readSep 8, 2019

--

In C/C++, main is the starting point to any program execution. All the program execution always starts from the ‘main’ with ‘int’ or ‘void’ are its return type.

void main()

The ANSI standard says ‘NO’ to use of ‘void main’ as its completely wrong. STOP using ‘void main’ from now on.There is nothing like void main() in C++ using it give you error. But in C it generate a warning message.

//C++ example of void main
#include <iostream>
using namespace std;void main()
{
cout << "Hello world!" << endl;
}

Compile error:

||=== Build: Debug in 1 (compiler: GNU GCC Compiler) ===|
\1\main.cpp|5|error: '::main' must return 'int'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

C example

//C example of void main
#include
void main()
{
printf("Hello world!\n");
}

Warning message:

||=== Build: Debug in 2 (compiler: GNU GCC Compiler) ===|
\2\main.c|4|warning: return type of 'main' is not 'int' [-Wmain]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Output:

Hello world!

Now you should be convinced to avoid void main, if not look at what stroustrup say about it.

Can I write “void main()” in C++?

The definition

void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to “the system” that invokes it. On systems that doesn’t provide such a facility the return value is ignored, but that doesn’t make “void main()” legal C++ or legal C. Even if your compiler accepts “void main()” avoid it, or risk being considered ignorant by C and C++ programmers.

In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

#include<iostream>	int main()
{
std::cout << "This program returns the integer value 0\n";
}

Note also that neither ISO C++ nor C99 allows you to leave the type out of a declaration. That is, in contrast to C89 and ARM C++ ,”int” is not assumed where a type is missing in a declaration.

Consequently:

#include<iostream>	main() { /* ... */ }

is an error because the return type of main() is missing.

main()

In C89, the main without return type has default type int. So, main is similar to int main in C89. But in C99, this is not allowed and one must use int before main (i.e. int main()).

int main()

So according to standard ‘int main’ is the best way to declare main function. Void main() has never been in C/C++ refer ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. for more details. It means that main function returns some integer at the end of the execution.

int main() { /* ... */ }int main(int argc, char* argv[]) { /* ... */ }

Above implementations shows main() with and without arguments but both have return type int. The return type int is a way for a program to return a value to the system that invokes it.

In C++, if explicit return statement (return 0) ignored than in that case, the value returned is 0, which means successful execution.

For example:

#include <iostream> 
using namespace std;
int main()
{
cout << "Return 0 without return statement\n";
}

Output:

Return 0 without return statement

Reference : https://www.externcode.com/int-main-void-main-and-main/

--

--