0

What is the difference between void main and int main ?

12th Jan 2017, 2:03 PM
Pahal Ram
Pahal Ram - avatar
4 Réponses
+ 3
In C++ int main requires the function to return an interger, 0 if the program terminated properly, 1 if it did not. A void main does not produce any return and does not let the computer know if the program ran as it was supposed to. This is where some programmes end up 'hanging'. where if it has terminated unexpectedly you my be able to try, throw and catch the error.
12th Jan 2017, 3:11 PM
Jason Hoffman
Jason Hoffman - avatar
+ 2
There must be something that should be explained before giving you the answer: the "main" function (in C++ and all other programming languages that work in the same way) is, well, how the program gets started: the input is given by the higher level that belongs to the O.S. It's a good habit to provide the O.S. with a confirmation of the correct execution of your program, that's why, as a convention, using 'int main' allows you to return a "success code" to the O.S., where it's assumed that the value 0 (return 0) means "success" and return 'something!=0' means "something went wrong...". If the signature of your main is 'void main' you're explicitly saying that you're not interested in providing a success code.
12th Jan 2017, 3:16 PM
Riccardo Cannistrà
Riccardo Cannistrà - avatar
+ 1
It is possible to return the code of the error if you use int main.
12th Jan 2017, 3:01 PM
Daniil Fomichev
0
Depend of the language. Some need the main return 0 for example, to indicate the end of the program, and some don't need it, for example C# : static void Main(string[] args)
12th Jan 2017, 2:12 PM
Guillaume BONHOMMEAU
Guillaume BONHOMMEAU - avatar