What does standard say about main return values range? Say only up to 255?
Because
int main(void){
return 256;
}
echo $? ; # out 0
What does standard say about main return values range? Say only up to 255?
Because
int main(void){
return 256;
}
echo $? ; # out 0
You return type
int
. You should be able to return any value that can be stored in anint
. The exact size of anint
is implementation-dependent, so I can't give you an exact range.Exit codes are a number between 0 and 255 inclusive on Unix like system. You can return anything but in Linux it's modded 256. Take a peek here for a good explanation on Linux return codes. There is also a Wikipedia article on the topic which talks breifly about exit codes for Windows.
The C standard do not impose particular limitation on exit codes, the paragraph about the return value of
main
delegates to the documentation about theexit()
function, which in turn says:which, apart from the
EXIT_SUCCESS
/EXIT_FAILURE
guidelines, basically means "do whatever you want".:)
As said in one comment, the fact that on POSIX systems only the lower 8 bits of the exit code are actually considered is just a UNIXism, deriving from how the
wait
syscall is designed (the exit status has to be packed in the lower 8 bits of thewait
return value), and has nothing to do with the C standard.A counterexample is Windows, where the whole value passed to
exit
/return
is considered (as long as it's not bigger than aDWORD
1, but I don't think they'll ever makeint
be bigger than aDWORD
, it would break a lot of code).1. Because the
GetExitCodeProcess
parameter reserved for returning this value is aDWORD *
.As others have stated, the C & C++ Standards don't constrain return values at all other than to state that (1)
main
returns anint
(which is of an implementation defined size) and (2) zero (orEXIT_SUCCESS
) is a successful return andEXIT_FAILURE
is a non-successful return. It does specify that amain
that does explicitly not return a value is treated as if it had returned zero.In this case, the interpretation of the return value is up to the process that waits on the process to complete (by calling wait, waitpid, or waitid). wait and waitpid are the older POSIX functions and they specify that only the least significant eight bits of the return value shall be available to a waiting parent process. The POSIX:2008 standard added waitid as a generalized wait method that has access to the full exit status of a child process.
After forking off a subprocess, another process calls one of the wait functions to sleep until the forked process is completed (e.g., returns from
main
, callsexit
orabort
or something). The wait and waitpid functions returns the status by way of a pointer to an integer. The caller extracts the actual exit status using theWIFEXITED(status_val)
andWEXITSTATUS(status_val)
macros. The latter is defined by POSIX and required to return the low-order 8 bits of the status argument. The waitid function uses a pointer to asiginfo_t
structure to return the process' status information. Thesi_status
member contains the full status value as described in Status Information.Basically, the values of the exit status are in the eye of the beholder. The ANSI/ISO specifications are open-ended. The POSIX suite has multiple ways to wait on a process to finish and fetch it's exit status. POSIX also defines spawn as a lighter-weight version of
exec
which has it's own set of constraints on exit status values. Shells have a habit of further restricting result values -- GNU's bash limits the return status to 7-bits and a POSIX-compliant shell limits exit status values to 8-bits. FWIW, most people agree that restricting your return values to be lower than 64 seems to be safe.The standard doesn't say.
0
,EXIT_SUCCESS
andEXIT_FAILURE
have (sort of) specified meanings. Anything else depends on the implementation.At the present time, most Unix-based systems only support 8-bit return values. Windows supports (at least) a 32-bit return value. I haven't checked whether 64-bit Windows supports a 64-bit return value, but I rather doubt it, since even 64-bit Windows normally still uses a 32-bit int.
C99 standard defines only 0 and 1. However, allows other values to be used.
See Exit Status wiki for more.