int main(), void main() and main(), Which one is best?

In C/C++, main is the starting point to any program execution. Like all other functions, main is also a function with a special characteristic that the execution always starts from the ‘main’ with ‘int’ or ‘void’ are its return type.

So, before we discuss which return type would be best in practice let’s see some fact of main function.

Importance of main() function:

  • main() function responsible for program starts and ends.
  • The return type of main() function give the status of a program to the Parent process, informing the success or failure of the program.
  • If we put other than int before main() function then we’re putting the program into the risk of not working properly.

void main()

The ANSI standard says big ‘NO’ to use of ‘void main’ as its completely wrong. If you are in love with it you should breakup now. STOP using ‘void main’ from now on.

There is nothing like void main() in C++ using it give you error ‘must return int’. But in C it generate a warning message. Though some compiler accepts “void main()” avoid it.

//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 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 i.e. returning 0 is a standard for the informing the system about successful execution of the program.

The standards contain below definition for main().

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

6 Comments

  1. Hello Bucky, I am Simon in Uganda, thank you for the tutorials, i have mastered a lot in cpp, i have studied for 9hrs using your youTube tutorial.
    Now how can i access the tutorials that follow this? The one containing coding for 3D games etc

  2. Hi Bucky , i have write a program . Can you advise me on the following program ? I never write a program before. I m keen to learn how to write C++ program. Please give comment n reply me by email.. Thank you

    please see the following

    void dish(int x) = (float x){
    dish 1 = $5.00
    dish 2 = $10.50
    dish 3 = $15.00
    dish 4 = $8.00

    dish(int x) = (float x);

    }

    int main() {

    cout << Hi can i have your order ? \n << We have dish 1 to 8 <> dish(int x) = (float x) ; << endl;

    cout << Thank you

    return 0

  3. Hi Bucky , i have write a program . Can you advise me on the following program ? I never write a program before. I m keen to learn how to write C++ program. Please give comment n reply me by email.. Thank you

    please see the following

    void dish(int x) = (float x){
    dish 1 = $5.00
    dish 2 = $10.50
    dish 3 = $15.00
    dish 4 = $8.00

    dish(int x) = (float x);

    }

    int main() {

    cout << Hi can i have your order ? \n << We have dish 1 to 8 <> dish(int x) = (float x) ; << endl;

    cout << Thank you

    return 0

Leave a Reply

Your email address will not be published. Required fields are marked *