What should we use main() or void main() or int main() ?

By | May 13, 2020

These two can be allowed by some compiler, but does not a standard. In both language C and C++, there is standard for it.

In case ‘main()’ or ‘void main()’ :
We can ignore return type only if a systems that does not provide such a facility.
But, if system provide return type facility then there can be error in use only ‘main()’, because the return type of main() is missing.

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

Or, using of ‘void main()’ is also legally not allowed in in either C or C++.

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

C and C++ standard :
In both language C and C++, there is standard for it. You may refer the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1
That’s why people should avoid to use only ‘main()’ or ‘void main()’.

It is suggested to use, in C :

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

And in C++ :

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

There can be more versions of main() but they must all have return type int. ‘main()’ returns a value to “the system”.

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

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

Note :
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.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.