Why we use return zero in main why not 1??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why we use return zero in main why not 1???

when ever main ended ,we are returning a value most of cases it is 0 not 1 if main asking for return value if it is void then there is no question of zero or one

14th Mar 2017, 6:22 PM
Swamy Kanuri
Swamy Kanuri - avatar
3 Answers
+ 17
"The return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signalled by a non-zero return but there is no standard for how non-zero codes are interpreted. ... It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0." Source: http://stackoverflow.com/a/204483
14th Mar 2017, 6:40 PM
Jafca
Jafca - avatar
+ 1
Because the exit code is answering the question "were there any problems?" Moreover, it's actually more than just a boolean: the number returned is a code that can specify what sort of error it was. IMHO, that's why Thompson and Ritchie decided to use 0 as "no problem" for "int exit()" in Unix. Then "return 0" is success in C/C++.
14th Mar 2017, 8:00 PM
Dare-devil
Dare-devil - avatar
+ 1
Because the exit code is answering the question "were there any problems?" Moreover, it's actually more than just a boolean: the number returned is a code that can specify what sort of error it was. IMHO, that's why Thompson and Ritchie decided to use 0 as "no problem" for "int exit()" in Unix. Then "return 0" is success in C/C++.
14th Mar 2017, 8:00 PM
Dare-devil
Dare-devil - avatar