int main() versus short main()?

Aug 19, 2013 at 10:56pm
In many books/tutorials I find that int main() is always used instead of short main().

Is it even possible to use short instead of int for your main() function?
If so, why not use short if you know that the main() function only returns -1 or 0?
Last edited on Aug 19, 2013 at 11:01pm
Aug 19, 2013 at 11:08pm
Because the C++ specification states that main shall return an int.
Returning anything else is not compliant with the specification.
Aug 19, 2013 at 11:21pm
Another reason is that int -1 and short -1 look different in binary.
Aug 20, 2013 at 5:51am
A fun link I like to dig up every now and then:
http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

Enjoy! :o>
Aug 20, 2013 at 9:27am
I see, thank you all :).
Aug 20, 2013 at 10:44am
Out if interest, where did you pick up the value of -1 ??

POSIX and Windows both provide the following defines (in stdlib.h/cstdlib)

1
2
#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1 


So in these case, main should preferably stick to returning 1 or 0, not -1 or 0

But implementations of C and C++ are free to define EXIT_SUCCESS and EXIT_FAILURE as other values. But 0 always means success.

Andy
Last edited on Aug 20, 2013 at 10:46am
Aug 20, 2013 at 11:01pm
BTW, don't return negative numbers from main(). It attracts bad karma.
Topic archived. No new replies allowed.