5

I am new to C++ and I'm reading a book called Big C++. In the book, all of the example programs I've seen so far end with return 0; before the final }. I can apparently make C++ programs run without return 0; at the end, so I'm wondering what purpose it serves. I am familiar with returning something from a method in java, but I don't understand why int main() would need to return 0 in C++. To get more to the point: Should I always end my main() with return 0; in C++? If not, when do I need to and when shouldn't I? What is return 0; telling the program to do?

In a related question, I am assuming that the main() in C++ is setting up the main function like the main method in java. Is that correct? Why is the main method being declared as an integer? Is that what is happening in the line int main() {?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Raddicus
  • 1,114
  • 1
  • 9
  • 9

4 Answers4

9

that represents exit code of application ,

For example if you launch it via some scripts and they want to determine if the program terminated normally exit code should be 0, non zero means some type of errors

speaking of Java

if you have specified int as return type you must have to return otherwise it won't compile, in case of void type it is zero unless there is an exception thrown out of jvm

jmj
  • 237,923
  • 42
  • 401
  • 438
7

In principle, any function returning something different than void has to be exited using a return statement (unless an exception is thrown): falling off the end of a function returning something else than void results in undefined behavior.

The beauty of C++ is that there are many exception to general rules: you can fall off the end of main() which behaves identical to using return EXIT_SUCCESS; which in turn is identical to return 0;. The reason for this special rule is something from the distant history of C and C++ as far as I understand (although I don't have any statement I could quote on this). In summary, although you don't need to return anything from main() using a suitable return statement is more consistent. I tend to omit it when putting code on slides for presentations but in real programs I always include a return statement.

BTW, if you want to indicate that a C++ program failed, you should probably return EXIT_FAILURE which is aside from EXIT_SUCCESS the only other value guaranteed to work. In practice other return codes also work but there isn't any guarantee.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • There is an explanation from the creator of C++: http://www.stroustrup.com/bs_faq2.html#void-main – enedil Aug 24 '14 at 23:18
  • @enedil: note that Bjarne's explanation does _not_ explain why the `return` statement can be omitted! The implicit `int` rule is not a sufficient reason as the return type could be omitted everywhere an `int` was returned but all other functions still required a `return` statement. – Dietmar Kühl Aug 25 '14 at 08:43
4

In case of C++, return 0; is implied. That is:

int main()
{
}

is equal to

int main()
{
    return 0;
}

Note that main which serves as an entry point for your app is the only one that allows you to omit the return statement for non-void return type.

Apart from that, the value itself is used to inform any other process (e.g. parental process) about the execution result, e.g. by reading it through posix waitpid.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
1

The main method is being declared as an integer, because you'd want it to return one. If the main method returns 0, it basically means everything went alright.