+ 1
In c++, why do people use "int main()" instead of "void main() "? Does the former have any advantages over the latter?
6 ответов
+ 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
+ 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).
+ 4
Beside all the previous excellent answers, compiler will throw an error ... 'main must return int'
+ 1
So why do they return 0 when there's no use to it?
0
It let's you end the program with return 0; Otherwise I don't know other advantages.