+ 1

In c++, why do people use "int main()" instead of "void main() "? Does the former have any advantages over the latter?

17th Mar 2018, 11:42 AM
binoy john
binoy john - avatar
5 Answers
+ 16
The latter should never be used. Your operating system calls the main function, and the C++ standard says that your main function is supposed to return a value to your OS which indicates the status of execution at the end of the program. Some old compilers may allow void return type for main, but it is not part of the C++ standard. http://www.stroustrup.com/bs_faq2.html#void-main
17th Mar 2018, 11:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
The return part of main is used by the OS to identify the status of the program after execution or at termination. Returning 0 will mean that the code raised no errors and its compilation and running was successful and it terminated without any errors. Any other number represents unsuccessful termination, and the codes are used to identify the type of error generated. Eg : 255 means a SIGSEGV (A Memory Access Error).
17th Mar 2018, 12:17 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
Beside all the previous excellent answers, compiler will throw an error ... 'main must return int'
26th Dec 2018, 7:57 AM
Shawn Gillis
Shawn Gillis - avatar
+ 1
So why do they return 0 when there's no use to it?
17th Mar 2018, 11:54 AM
binoy john
binoy john - avatar
0
It let's you end the program with return 0; Otherwise I don't know other advantages.
17th Mar 2018, 11:52 AM
Aaron Eberhardt
Aaron Eberhardt - avatar