21
votes

Why is it not necessary to include the return statement while using int main() in some compilers for C++? What about Turbo C++?

7
AFAIK, Turbo forces void main (which is invalid C++) and returns 0 for you.chris
I'm pretty certain that Turbo C++ conforms to exactly zero of the C++ standards in existence. As far as I'm aware, development of Turbo C++ had stopped at the time of 1998 when the first C++ standard was produced.Mats Petersson
I hope you're asking out of historical curiosity, and not because you're planning to use a twenty-year-old compiler for anything.Mike Seymour

7 Answers

44
votes

In C++, and in C99 and C11, it is a special rule of the language that if the control flow reaches the end of the main function, then the function impliclty returns 0.

26
votes

In C++ and C99/C11, without a return statement in main function, it's default to return 0;

§ 3.6.1 Main function

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;

also read wiki page C/C++ main function

In case a return value is not defined by the programmer, an implicit return 0; at the end of the main() function is inserted by the compiler; this behavior is required by the C++ standard.

13
votes

main must return an int, some compilers, including Turbo C++, may allow other return values, notably void main, but it's wrong, never use that.

However in C++, if you don't explicitly return a value in main, it's the same as return 0;

C++11 §3.6.1 Main function section 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;

Note that for C, this is only supported in C99 and later, but not supported by C89.

4
votes

The standard says that main will return 0 if there is no return statement. From the draft C++ standard section 3.6.1 paragraph 5:

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;

The C99 draft standard in section 5.1.2.2.3 says:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0.

From what I can tell the last version of Turbo C++ is quite old and I can not find anything that defines which standard if any it supports.

1
votes

TC will return EXIT_SUCCESS no matter you explicit specify or not

0
votes

Other than conforming to an arbitrary language standard, there is no compelling reason to return a value when the main function ends.

It is only because processes on the popular operating systems have a notion of a return code for a process that has terminated. It is not hard to imagine an operating system where they need not return any value. You can also imagine a world where 0 is implicitly returned by all processes unless otherwise overridden -- in which case, you can delegate the return code decision to a system call. (In fact, such a system call already exists in the form of C's exit function).

0
votes

As other people have stated, some compilers don't require you to explicitly return 0; however, it is always a good idea to (if possible). I would discourage the use of any compiler that disallows you from returning from your main function.

I would like to note that the return value from int main is very important, and actually has a use.

The return value from main is sometimes referred to as the exit status or error code from your program, zero indicating that it completed successfully. Other programs and scripts can access this code to determine if your program completed successfully.

More information here: http://en.wikipedia.org/wiki/Exit_status