Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

main

return

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

like image 757
Raddicus Avatar asked Aug 24 '14 21:08

Raddicus


People also ask

Why do we use return 0 at the end in C?

return 0: A return 0 means that the program will execute successfully and did what it was intended to do.

Does return 0 end the program in C?

Use return 0 within the main function We are returning 0 because the main function produces a numeric value (int main()). The term return is being utilized to return a result with a function. This signifies that the program have been completed accurately, and we can use the return statement to end the main function.

Does return 0 end a loop?

In general, a return statement will terminate a loop (that's normally break to just end the loop) by returning flow of control to a method caller; in the case of main() that is the operating system.

Is return 0 the same as Exit 0?

In C++, what is the difference between exit(0) and return 0 ? When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.


2 Answers

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

like image 111
jmj Avatar answered Sep 30 '22 15:09

jmj


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.

like image 27
Dietmar Kühl Avatar answered Sep 30 '22 14:09

Dietmar Kühl