21

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

Suman
  • 9,221
  • 5
  • 49
  • 62
ajarmani
  • 269
  • 1
  • 2
  • 9
  • AFAIK, Turbo forces `void main` (which is invalid C++) and returns 0 for you. – chris Aug 23 '13 at 12:30
  • 26
    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 Aug 23 '13 at 12:36
  • 12
    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 Aug 23 '13 at 12:40

7 Answers7

45

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 5
    @H2CO3 how is it horrible? – Kolyunya Aug 23 '13 at 12:30
  • 17
    @H2CO3 no it's not – No Idea For Name Aug 23 '13 at 12:30
  • @Kolyunya Almost as horrible as `void main()`. –  Aug 23 '13 at 12:31
  • 1
    Maybe because not enough people know about it, but other than that, I don't see much of a reason. – chris Aug 23 '13 at 12:31
  • 3
    @NoIdeaForName Well, I hate when one lets the function fall off. Whoever relies on this in `main()` will do the same in other functions. –  Aug 23 '13 at 12:31
  • @H2CO3 I don't think that's generally true. Besides, in the vast majority of other situations the compiler will complain about it anyway. – Marc Claesen Aug 23 '13 at 12:32
  • 7
    @H2CO3 "Whoever relies on this in main() will do the same in other functions." Definitely not! I could argue it the other way: whoever returns `0` needlessly is likely to be lacking a clue, so they would be likely to fall off the end of a function. They probably write functions that are 500 lines long too :) – juanchopanza Aug 23 '13 at 12:33
  • 7
    It's a matter of opinion, but I'm with @H2CO3 here. IMO this exception to the usual rules is totally unwarranted, and as any useless rule I find it a pain more than anything else. – syam Aug 23 '13 at 12:34
  • @juanchopanza Well, you definitely haven't worked with the code of those people that I have, it seems. Oh, and I always `return 0;` at the end of `main()`. (Yeah, you'll now tell me that I'm lacking a clue...) –  Aug 23 '13 at 12:34
  • 2
    IMO you really should not rely on this; many compilers will issue a warning if you don't return anything from a non-void function, and I really like to compile with `-Wall -Werror` flags, so compilation will fail because of this useless exception... – nikolas Aug 23 '13 at 12:39
  • 14
    Let's face reality. return from main() mostly has a meaning then application intended to be run from batches/scripts/whatever else which rely on return code. People who write such application/utilities are aware of that and I don't think there is an issue about that, but who on the Earth care which return code I get from Microsoft Word or Visual Studio.. Ok, I know and I agree that it's a good code practice, but I don't think it's most worst thing you can do.. – evilruff Aug 23 '13 at 12:40
  • 5
    @nijansen, Any compiler that refuses to compile not returning something from `main` can't be very good... – chris Aug 23 '13 at 12:44
  • 15
    @evilruff What you say makes sense, but I'd rather allow `void main()` (and have the runtime "convert" it to `int main()` returning 0) rather than having this weird exception to the return rules. But maybe that's just me. – syam Aug 23 '13 at 12:46
  • Looking back, it's not even about C/C++ or whatever language. it's an OS convention have an ability to retrieve a return code for whatever purpose it needs it.. somebody still remember 4Ch function of interrupt 21h? I think main() function as any entry point place itself is a pretty 'exceptional' (take WinMain() for example..) sometimes it's just forgotten that our programs exist in real world and standards still should rely on real things, haven't being a 'spherical horse in space'.. i.e. DLL which also a software in a way, but has completely different rules for loading/entry points etc.. – evilruff Aug 23 '13 at 12:56
  • What I am saying is that 'return code' for main() in a way out of the scope for language itself..its about about compiler for specific environment to decide what to do with that.. that's why main() is exceptional and i don't see really issues with 'special cases'. – evilruff Aug 23 '13 at 13:00
  • 4
    @H2CO3 `main` is not like any other function anyway (it must return and `int` and has at least two possible parameter-list signatures but you can't overload it, you can't explicitly call it from code, you can't take its address...) and every "decent" C++ programmer should know (or learn) that it is special. (Now, "real" code often returns an error code (nonzero) from `main` in case of failure, so then it's just "natural" to explicitly return zero in case of non-failure.) – gx_ Aug 23 '13 at 13:10
  • I don't have my K&R in front of me, but I don't think they returned 0 from every main() they wrote. So worst case, it's some kind of deprecated behavior that should still be supported. – Jiminion Aug 23 '13 at 14:39
  • 1
    I agree that it's horrible. The compiler has to have special hack behaviors for compiling the internals of `main` just for the sake of sloppy programmers. Other `int`-returning functions will not return `0` if control reaches their end and there is no return statement. So, the rule should apply to all functions, or to no functions at all. – Kaz Aug 23 '13 at 15:06
  • @Kaz main already is center of a lot of "hacks" that make it the official entry point of a program, adding a rule that the default exit state of a program is success instead of requiring unnecessary verboseness is not any more horrible. (main does not even qualify as function - you are not allowed to call it yourself) – josefx Sep 01 '13 at 11:57
  • 1
    @josefx You can call main in C, but not in C++. So that is just another horrible hack, albeit specific to C++. – Kaz Sep 01 '13 at 15:25
26

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.

billz
  • 44,644
  • 9
  • 83
  • 100
13

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.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
4

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.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

TC will return EXIT_SUCCESS no matter you explicit specify or not

MG.
  • 449
  • 6
  • 15
0

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).

Tac-Tics
  • 1,895
  • 13
  • 19
0

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

Tyzoid
  • 1,072
  • 13
  • 31