why do people define main() as int and not void?

and then write return 0; or return 1; or return -1; or return EXIT_SUCCESS;

what is the point of this?

int main() is the required format under the ANSI/ISO standards. If you return anything but an int, it can lead to system issues. Some compilers will even complain if you try to return anything but an int.

As far as value is concerned, zero (0) is considered the "universal" success indicator. It says the program finished with 0 errors. EXIT_SUCCESS is really nothing more than a macro/variable that contains the value 0. If you return any other value, it tells the o/s there was an error and the program terminated abnormally.

EDIT:
Oops.... WaltP beat me to it.

Edited 6 Years Ago by Fbody: n/a

so if you return -1 its gonna throw exception or what?

and how is perfectlly normal in java to say public static void main( String args[]) {} ???????

>So if you return -1 its gonna throw exception or what?
-1 is a valid return value, but what it means is implementation-defined.

>how is perfectlly normal in java to say public static void main( String args[]) {} ???????
When you design a programming language, you make the rules. It's really as simple as "because I said so" when it comes to things like this. C++'s language spec says to return int, so we return int.

Edited 6 Years Ago by Narue: n/a

>pretty stupid reason I must say
A lot of things seem stupid when you're too ignorant to understand.

This article has been dead for over six months. Start a new discussion instead.