+ 5

Why do we use int main()? Y not void main()?

25th Jul 2017, 9:11 AM
yash Chitty
yash Chitty - avatar
3 Answers
+ 8
void main() is obsolete and no longer supported by modern compilers. In the latest standard, main() must return int.
25th Jul 2017, 9:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
That's because main returns 0 by default at the end of the function. We can manually type "return 0;" at the end of the function if we want, because it's basically the same thing.
19th Nov 2018, 5:27 PM
Paul Mihali
Paul Mihali - avatar
+ 1
the return int value gives clue to the calling process about the way the program ended. A program that returns a 0 value successfully terminated when other values stand for different error cases. You can then use this return value in, for example, bash scripts to test if your program execution succeeds. For this reason the C++ standard defined the main function with an integer as return value. void main() may work with some compilers but has never been valid.
25th Jul 2017, 11:07 AM
MOULHERAT Matthieu
MOULHERAT Matthieu - avatar