Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

In C/C++, why is it recommended/strongly encouraged that the main function has an 'int' return type (void in some systems), and that it returns 0 (or at least calls the exit() function from stdlib.h).

OpenStudy (anonymous):

Here is your answer: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3 Have fun ^^

OpenStudy (anonymous):

Also to add this on: The standard dictates that every C/C++ program must return a value to indicate its success. Void doesn't return any value, it destroys the value.

OpenStudy (rsmith6559):

It probably goes back to Unix. A program exits with 0 on success, and other values, usually (if I remember correct) < 127 are programmer defined error codes.

OpenStudy (anonymous):

What is the difference between\[\small\text{return 0;}\]and\[\small\text{exit(EXIT_SUCCESS);}\]

OpenStudy (anonymous):

in the main function, at least :-D

OpenStudy (rsmith6559):

EXIT_SUCCESS is just a defined value, probably 0. return would return a value, exit would probably be OS dependent.

OpenStudy (farmdawgnation):

I believe all GNU compilers will define EXIT_SUCCESS as 0 for you as a part of the standard library. The reason for this returning of values is as a quick and dirty way to indicate the status of what happened in your code. Consider the situation where your program was invoked by another piece of software. That piece of software may not be able to understand the messages you're sending to the console, but if you exit with a nonzero value - it will know something went sideways and usually display the error message to the end-user. The easiest example I can think of this is the make utility. When make is calling gcc to compile your code - it doesn't understand error messages output by gcc when you have a syntax error. But it does know what it means when gcc returns some value other than zero on exit, something went wrong.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!