Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does int main() {} compile?

(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++?

like image 318
Jeff Linahan Avatar asked Aug 22 '08 12:08

Jeff Linahan


People also ask

What is the problem in declaring int main ()?

int main() doesn't include any list. So gcc is complaining about an old-style (K&R) function definition...that would allow main() to take any number of arguments. That's not one of the standard-sanctioned forms.

Is int main () a function declaration?

Nope. You can't call it anyway. You only need forward declarations for functions called before they are defined. You need external declarations (which look exactly like forward declarations on purpose) for functions defined in other files.

Why does it have to be int main?

The short answer, is because the C++ standard requires main() to return int . As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished.

Why did we use int before the main () function?

Because the main function returns type integer,i.e either '1/program ran successfully' or '0/program compile error '.


2 Answers

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.

like image 56
sparkes Avatar answered Oct 11 '22 18:10

sparkes


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.

like image 18
On Freund Avatar answered Oct 11 '22 17:10

On Freund