SK90
SK90

Reputation: 79

What is the convention for main function return values in C++?

I was reading sample code and found that for error messages they used return -1; and for the main function they used return 0;. However, when I substituted the main function return value to return 10000;, it still built and run with no errors. Does this mean that return values can be arbitrarily chosen and has no meaning for the computer (Excluding the case where you are using the return value for some user-defined operation)? What is the usual convention in assigning return values for different cases?

Upvotes: 4

Views: 2585

Answers (5)

Pete Becker
Pete Becker

Reputation: 76285

The portable return values are EXIT_SUCCESS, EXIT_FAILURE, and 0 (which returns the same value as EXIT_SUCCESS. Those manifest contacts are defined in <cstdlib>. The meanings of any other values are system-specific.

Upvotes: 1

kbal
kbal

Reputation: 11

By convention a return value of 0 in main is often hint for the OS and users that the program was executed successfully. Other values can be used to represent different error codes or different paths of successful execution specific to the program. You can choose them freely.

For other functions in a program the interpretation of return values is up to the developer.

Upvotes: 1

user6160478
user6160478

Reputation: 119

On systems that use libc, which is pretty much the majority of Unix systems, the following things happens when a program exits:

  1. Functions registered with atexit or on_exit are called in reverse order
  2. All open streams are closed, writing out any buffered output data
  3. _exit is called, terminating the program

Only the low-order 8 bits of the exit status are kept, which truncates it to a value between 0 and 255. How the return status is treated depends on the parent process. For example, if the parent process was Bash, values between 0 and 127 are user-defined values, while 128+ are treated specially.

Upvotes: 1

Pooya
Pooya

Reputation: 6136

The return value from a main function (the application) can be used in other applications to indicate that how the program is terminated. Traditionally 0 is used if the program terminated successfully and other integers (negative or positive) indicates there were some problems or other conditions.

These are different from application to application and you can find these on their manuals to find what each return value means.

For example you can see grep command manual (http://www.gnu.org/software/grep/manual/html_node/Exit-Status.html) it states that exit value 0 is if a line has selected, 1 means no line has selected and 2 means there was an error

Upvotes: 7

jpo38
jpo38

Reputation: 21514

Returned value is specified by function definition:

ret function(params);

Then, any value of type ret will be accepted by the compiler.

Example:

int main();

Can return any value of type int (between -INT_MAX and +INT_MAX). Values outside this range can be automatically casted by the compiler. However, non int values (return std::vector<int>() for example) will lead to compiler errors.

Specifically, for the main function, you can return any value. Convention is to return 0 when no error occured, anything else when an error occured. Error values can be positive or negative, as you want.

Upvotes: 0

Related Questions