The standard return code when everything goes right is 0, but what about using any other number something happened? Are there "ranges" depending on the kind of problem you want to express or is totally up to the programmer?
By convention in the standards:
0 success
1-125 a program specific failure
126 when trying to execute a command, a file was found with the
specified command name, but it was not executable
127 when trying to execute a command, no file was found with the
specified command name
>128 the command was terminated by a signal (signal number being exit
status - 128)
Some systems have considerably more conventions for the 1-125 exit status range (for example, look at /usr/include/sysexits.h
on BSD systems).
And what about negative numbers? I've seen the return -1;
quite often
For a return from main(), the return code is equivalent to a call to exit() with that return code. And from the standards, the description of exit(status)
is:
For a return from any other function, the return value is determined by how that function is declared and the documented behavior of that function. For example, in C, fopen() returns a FILE pointer or NULL; sprintf() returns a pointer to a string or NULL, strtoll() returns a signed long long integer, strtoull() returns an unsigned long long integer, etc.