Aug 19, 2013 at 10:56pm UTC
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 UTC
Aug 19, 2013 at 11:08pm UTC
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 UTC
Another reason is that int -1 and short -1 look different in binary.
Aug 20, 2013 at 10:44am UTC
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 UTC
Aug 20, 2013 at 11:01pm UTC
BTW, don't return negative numbers from main(). It attracts bad karma.