0

Why is it so that even without using return 0, the program runs? What's the actual use of return 0

28th Feb 2016, 5:19 AM
Ritwik Madhukalya
4 ответов
+ 3
Expanding on Sumudu's answer a bit.. if you are using "int main()" in your program and leave out the "return 0;" the compiler will usually add it for you nowadays.
27th Oct 2016, 1:29 PM
Neen
Neen - avatar
+ 2
I'm afraid "Abilash A"'s answer might misguide inexperienced programmers. "return 0" does not mean it has nothing to return (actually that's what 'void' does). In programming (and mathematics etc..), "zero" does mean something. In certain practices, e.g. in main() function, function return codes indicate an error code. In this case, an error code of "0" (return 0) means "no error". That is the program exited successfully. Which certainly gives an important information. Any dependent programs will decide what to do on the return code of the previous program. Alternatively, if you return nothing or "void" (notice this is completely different to returning 0), then the person (or the program) who executed this particular program will have no idea about the exit status of the program. Usually this is considered a bad practice. Return status is very much helpful if you are writing an automated script to invoke multiple tools. Consider following pseudo script. e.g. execute_a_program_to_generate_a_text_file # a log file, in a real scenario look_for_a_line_in_the_file It should not proceed to second line if the text file has not generated. But how do you know that? yes, one option is checking the return code. e.g. (improved version) err_code=execute_a_program_to_generate_a_text_file # variable err_code contains the return code if err_code == 0: look_for_a_line_in_the_file else: print "failed generating file:
16th Oct 2016, 7:18 AM
Sumudu
Sumudu - avatar
+ 1
hi, return 0 ; statement just implies that the program ended in an expected way (successfully)
18th Mar 2016, 12:23 PM
MOUZONG DJAM Andreas
MOUZONG DJAM Andreas - avatar
+ 1
return 0 is used to mention the function int main() has nothing to return after the execution. If void main() function is used instead of int main() there is no need for return 0 statement. void means nothing or null.
6th Apr 2016, 5:25 AM
Abilash A
Abilash A - avatar