y not void main() ?

Nov 23, 2011 at 12:29pm
I have heard many times dat void main() is not standard c++ and one should use int main() and return 0.....it makes no difference ..den wats d big deal ??
Nov 23, 2011 at 12:58pm
http://www2.research.att.com/~bs/bs_faq2.html

(Search for "Can I write "void main()"?")
Last edited on Nov 23, 2011 at 12:58pm
Nov 23, 2011 at 1:01pm
Returning an int is in the spec and thus the main function is declared to return an int.
It does make a difference sometimes, often it won't as the compiler will be able to deal with it being void for compatibility's sake and perhaps issue a warning.
Numbers other than 0 can be returned if you want to, 0 is returned when the program terminates normally, for other situations you might want to use EXIT_FAILURE from cstdlib.
Nov 23, 2011 at 1:02pm
it makes no difference


Your compiler is broken. It should at the very least whinge. Get one that works properly.

It makes a difference. Just because you can't see something doesn't mean it doesn't exist.
Last edited on Nov 23, 2011 at 1:28pm
Nov 23, 2011 at 1:29pm
@gaminic....tanx
Nov 23, 2011 at 2:51pm
1. It is explicitly forbidden by the C++ language specification
2. It may crash some non-mainstream systems: http://users.aber.ac.uk/auj/voidmain.shtml (mainstream systems had to accomodate for this common error)
Nov 23, 2011 at 4:51pm
It's one more letter to type so the real question is Y WUD U???
Nov 23, 2011 at 5:03pm
@OP if you're developing a command line utility then the return value of main is often used as the return value of the utility. For example if you have a fatal error main should return -1, if successful it should return 0, then if you have some other error cases you want to return out you can return 1,2,3,4,..., 2,147,483,647
Topic archived. No new replies allowed.