+ 1

Why do we have to return always 0 in case the return type of the main() is int. What will happen if we return some integer other than 0?

16th Mar 2016, 9:29 PM
Rahul Singh
Rahul Singh - avatar
5 Answers
+ 1
What you return doesn't intrinsically mean anything as has been stated before, but most OSs take returning 0 to mean that the program has ended with no abnormalities and a non-0 return to mean an abnormal error has occurred. This is mainly so the OS knows to display error notifications and go through the necessary procedures it needs to go through after a program encounters an error.
25th Jun 2016, 12:19 AM
Connor Marshall
Connor Marshall - avatar
You can declare your main () as a void function, that way no return value is needed. (This is not very used though) Other than that you can use any integer as your return value. Another case would be for testing your code, like in a switch. Every case could have a different return value as it ends de program. This way you could know precisely which case was evaluated coming from the return int. Hope this helps!
14th Jun 2016, 5:07 AM
Jose Carlos Solorza
Jose Carlos Solorza - avatar
we use return 0 by default to tell that execution has comp successfully.. but according to our program.. if it had some other value to be returned then we can return other values too.. so it depends on the program..
15th Jun 2016, 1:51 AM
Mahrukh Ahmed
Mahrukh Ahmed - avatar
as int has a return type , so you have to return something to the main function for successful execution. return 0 is a standard way of writing, you may return any integer value
17th Jun 2016, 6:42 PM
Debabrata Karan
Debabrata  Karan - avatar
- 1
returnt 0 is the default return type...however if u have a value to be return u can use the value or result to be return
1st Apr 2016, 11:45 AM
Ephrem Kinfe
Ephrem Kinfe - avatar