+ 5

Can someone please explain:

Why in C++ the main method has a return type of int and why it need to return a value? Thank you very much.

7th Jan 2017, 9:29 AM
Shahar Levy
Shahar Levy - avatar
2 Réponses
+ 4
Think about using your code in a command line (e.g. in a bash or a batch script): if you call your compiled code you call the main method giving to it the arguments passed in the string parameter. The result of your code would be passed to the script to test if an error is occurred. You could pass always 0 but you would lose the functionality of the result that in case of error would stop the script.
7th Jan 2017, 9:40 AM
Michael Isac Girardi
Michael Isac Girardi - avatar
+ 3
generally the return value of the main indicates how the programme exited. if it returns a zero then the programme returned normally and a non-zero return signifies an abnormal exit
7th Jan 2017, 9:40 AM
Brendan Rovholo
Brendan Rovholo - avatar