Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Why do we use return 0 in C language if we can run a program or do whatever with program without it ?
Posted
Updated 17-Oct-15 8:08am
v3
Comments
PIEBALDconsult 17-Oct-15 14:08pm    
Discipline.
Member 12066430 17-Oct-15 14:13pm    
discipline ??
[no name] 17-Oct-15 14:26pm    
"return 0"?

if (program_executed_fine)
return 0;
else if (program_had_error)
return everything but not 0;
Afzaal Ahmad Zeeshan 23-Oct-15 16:49pm    
Correct, but why not return a character pointer, like, return "executed safely";? :-) That was the question.
[no name] 23-Oct-15 16:51pm    
What?

That was the Q:
"Why do we use return 0 in C language if we can run a program or do whatever with program without it ? "

How about Google?
E.g. see http://bash.cyberciti.biz/guide/The_exit_status_of_a_command[^].
Note that C was developed for porting Unix to various computers those days. So, the concept of exit code of any command may have its root in Unix's shell concept.
Regards
Andi
 
Share this answer
 
Comments
Zoltán Zörgő 17-Oct-15 18:13pm    
That's the idea. +5
Andreas Gieriet 17-Oct-15 18:17pm    
Thanks for your 5, Zoltán!
Cheers
Andi
The return indicates how a program terminated, either successfully or otherwise. Since the error state likely needs more of an explanation than if a program finishing successfully, 0 was chosen to indicate success (i.e. you only need more details if you've failed to execute).

Most programs have an assortment of failed return values that correspond to some error codes (for example, 1-failed to open file, 2-failed to write to file, 3-file not found, so on). This allows the operator and/or script that's executing your application to know why it failed, not simply that it has failed.
 
Share this answer
 
most programmes return error code; so that the user can fix or track the error if it occures.

so '0' means no error.
 
Share this answer
 
Simply we can thought as

your program always run under operating system like windows. simply
C++
int main()

function is a integer return type. if the return is 0 it's send a message to os is that the program is OK no error is generated while running the program and exit is success.

if you use
C++
void main()
you don't have to return anything because it is void return type.OS automatically manage it.but you unable to track that the program run correctly or not.(i am not sure about it , i told it from my experience and learning )
 
Share this answer
 
v3

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