Search code examples
c++returnprogram-entry-point

When should I end a C++ program with 'return 0'?


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() {?


Solution

  • 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