Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
2.45/5 (7 votes)
See more:
Almost in every program we code there is mostly
C++
return 0 ;
in the end of main function. I know This means all things are going on way. But what does it mean? What exactly are there at the background of this ? Thanks.
Posted
Updated 25-Aug-21 3:09am
Comments
z3ngew 6-Dec-13 3:40am    
if your main function is of type (void) you will not have to write it
Aescleal 6-Dec-13 3:56am    
If you use void main() your code isn't guaranteed to compile or do what you want. So don't. Ever.
z3ngew 6-Dec-13 6:41am    
I didn't know that, Thanks for the info.

In every C program you have to use return return 0; (or return -1;, or whatever... ), because the main function signature requires it.
In a C++ program the statement is optional: the compiler automatically adds a return 0; if you don't explicitely return a value.

The return value is the exit code of your program, the shell (or any other application that ran it) can read and use it.
The 0 exit code is a widely accepted convention for 'OK the program execution was successfull'.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Apr-14 11:35am    
5ed.
—SA
Afzaal Ahmad Zeeshan 15-Aug-17 10:37am    
5ed; I wonder who — and why — downvoted it.
CPallini 15-Aug-17 12:49pm    
It is a rather old post. Thank you very much.
In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application.

See : http://en.wikipedia.org/wiki/Exit_status[^]

On most operating systems returning 0 is a success status like saying "The program worked fine". In C++ it is optional to type "return 0;" at the end of the main function and the compiler includes it automatically.

In stdlib.h the macros EXIT_SUCCESS and EXIT_FAILURE are defined like this :
C++
#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1


These 2 macros can be used as the argument to the exit function declared in stdlib.h and they can also be used as the return value of the main function.
 
Share this answer
 
v2
the return value is useful to check the status when the application exit.
return 0 means no error.
 
Share this answer
 
The return value can be used outside the program by the caller program, script or command. Provided two programs:
the program f1
C++
int main(){return 0;}

and respectively f2
C++
int main(){return 1;}

If you call them like this, you get this output f1 success
Shell
$ ./f1 && echo f1 success || echo f1 failure

When you call f2 in similar way the output is f2 failure
Shell
$ ./f2 && echo f2 success || echo f2 failure

Above chaining is possible because of the return code, also exit code.
Invoke as yourprogram && otherprogram if you want otherprogram to be called when yourprogram in case it finished successfully. Or call as yourprogram && otherprogram if you want otherprogram to be called when of failure of yourprogram
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900