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

Updated

There is question that what should we use void main() or int main() ?

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

Or,

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

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

It is suggested to use,

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

In C, and

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

There may be 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:

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:

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

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

Refer – http://www.stroustrup.com/bs_faq2.html#void-main

Comment
Next tutorial
Mithlesh Upadhyay Published 20 Nov 2024 · 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.

Topics:

Related tutorials

  • Urban Company interview experience for campus hiring

    Urban Company (formerly UrbanClap) visited our campus for hiring FTE. They were open for students in CS, IT, ECE, EE and the CGPA criteria was > 7. The…

    2 min read
  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • Delete an element or a range from a set in C++ STL

    Delete an element from the Set by passing its value : Approach : In this method, an element is deleted by using the erase function and calling it…

    3 min read
  • 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
  • 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