Click here to Skip to main content
14,603,017 members
Rate this:
Please Sign up or sign in to vote.
See more:
Almost in every program we code there is mostly
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 24-Nov-19 10:18am
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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'.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 4

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 :
#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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

the return value is useful to check the status when the application exit.
return 0 means no error.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100