1

In short, I'm using Microsoft Visual Studio 2012, programming in C++. My main method has a while(1) repeating a bit of code, and never leaves the loop.

Microsoft Visual Studio is allowing me to compile without a return value for the main method, even though it's declared int main(int argc, char** argv). Is this standard for other compilers, or just a "feature" of Microsoft Visual Studio?

I realize I never get to the code that returns a value, but should I just thrown on a return 0; at the end anyway?

  • 3
    If you put in a `return 0` and compile with optimizations and maximum warnings, it'll complain about unreachable code. – Mysticial Jun 06 '14 at 21:48
  • 1
    Visual Studio 2013 will actually accept things like `double *****main(){ }`. – nwp Jun 06 '14 at 21:56

2 Answers2

7

By the standard, the main function in C++ implicitly returns 0, even if there is no return statement in it.


The exact reference (from n3797): 3.6.1.5

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

Csq
  • 5,775
  • 6
  • 26
  • 39
0

The return 0; statement can be omitted from main even though its return type is int. It is defined by C++ standard. In this case it returns 0 by default.

haccks
  • 104,019
  • 25
  • 176
  • 264