Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value range of the main function

Tags:

What does standard say about main return values range? Say only up to 255?

Because

int main(void){ return 256; } echo $? ;  # out 0 
like image 241
Nyan Avatar asked Mar 01 '11 00:03

Nyan


People also ask

What is the return value of main function?

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value.

What is the return value of main () in C?

The return value for main is used to indicate how the program exited. If the program execution was normal, a 0 return value is used. Abnormal termination(errors, invalid inputs, segmentation faults, etc.) is usually terminated by a non-zero return.

What is return type of range?

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

What is the main function of value?

Values provide goals or ends for the members to aim for. 2. Values provide for stabilities and uniformities in group interaction. They hold the society together because they are shared in common.


2 Answers

The standard doesn't say. 0, EXIT_SUCCESS and EXIT_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.

like image 160
Jerry Coffin Avatar answered Nov 02 '22 16:11

Jerry Coffin


As others have stated, the C & C++ Standards don't constrain return values at all other than to state that

  1. main() returns an int (which is of an implementation defined size), and
  2. zero (or EXIT_SUCCESS) is a successful return and EXIT_FAILURE is a non-successful return.

It does specify that a main() 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, the parent process calls one of the wait*() functions to sleep until the forked process is completed (e.g., returns from main(), calls exit() or abort() or something). The wait() and waitpid() functions return the status by way of a pointer to an integer. The caller extracts the actual exit status using the WIFEXITED(status_val) and WEXITSTATUS(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 a siginfo_t structure to return the process's status information. The si_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 its 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.

like image 40
D.Shawley Avatar answered Nov 02 '22 17:11

D.Shawley