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

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 13 May, 2020 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read