0

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?

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Narue
0

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 by Fbody: n/a

0

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[]) {} ???????

0

>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 by Narue: n/a

2

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.