37

(I'm using Visual C++ 2008) I've always heard that main() is required to return an integer, but here I didn't put in return 0; and and it compiled with 0 errors and 0 warnings! In the debug window it says the program has exited with code 0. If this function is named anything other than main(), the compiler complains saying 'blah' must return a value. Sticking a return; also causes the error to appear. But leaving it out completely, it compiles just fine.

#include <iostream>
using namespace std;

int main()
{
    cout << "Hey look I'm supposed to return an int but I'm not gonna!\n";
}

Could this be a bug in VC++?

Helen
  • 87,344
  • 17
  • 243
  • 314
Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56

4 Answers4

72

3.6.1 Main function

....

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

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

and

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

.... and it continues to add ...

5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling 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;

attempting to find an online copy of the C++ standard so I could quote this passage I found a blog post that quotes all the right bits better than I could.

Community
  • 1
  • 1
sparkes
  • 19,343
  • 5
  • 39
  • 46
  • g++ will throw an error at you by default for doing this. most other compilers do "return 0" instead – wakingrufus Apr 29 '09 at 01:06
  • 4
    Older versions of GCC actually returned garbage in some instances if you didn't have a return statement in main(). – Mark Bessey Jul 17 '09 at 20:23
  • 9
    @wakingrufus, what version of g++ are you using? I'm getting the correct behavior as described by sparkes. – Kevin Jul 17 '09 at 20:34
  • Note that the standard quoted is C99, not C89 or C++98, yet MSVC mostly implements C89 and not C99 or C11 when used as a C compiler. However, you can also consult the Microsoft documentation — which is where `void main(…)` is defined as OK (in C). – Jonathan Leffler May 17 '14 at 20:17
18

This is part of the C++ language standard. An implicit return 0 is generated for you if there's no explicit return statement in main.

On Freund
  • 4,376
  • 2
  • 23
  • 30
3

I'm pretty sure VC++ just inserts a return 0 if you don't include one in main functions. The same thing can happen with functions too, but in those cases at least you'll get a warning.

bradtgmurray
  • 13,683
  • 10
  • 38
  • 36
  • 5
    It is undefined behavior to fall off the end of a non-void function. Only main is special with implicit return. – Fred Nurk Dec 01 '10 at 16:18
2

Section 6.6.3/2 states- "Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.".

An example is the code below which at best gives warning on VS 2010/g++

int f(){
   if(0){
      if(1)
         return true;
   }
}

int main(){
   f();
}

So the whole point is that 'main' is special as the previous responses have pointed out.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129