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?

Recommended Answers

All 6 Replies

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.

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.

pretty stupid reason I must say

thank you all,thread solved!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.