Why Default Return Value of Main Is 0 and Not Exit_Success

Why default return value of main is 0 and not EXIT_SUCCESS?

Returning zero from main() does essentially the same as what you're asking. Returning zero from main() does not have to return zero to the host environment.

From the C90/C99/C++98 standard document:

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.

Should I return EXIT_SUCCESS or 0 from main()?

EXIT_FAILURE, either in a return statement in main or as an argument to exit(), is the only portable way to indicate failure in a C or C++ program. exit(1) can actually signal successful termination on VMS, for example.

If you're going to be using EXIT_FAILURE when your program fails, then you might as well use EXIT_SUCCESS when it succeeds, just for the sake of symmetry.

On the other hand, if the program never signals failure, you can use either 0 or EXIT_SUCCESS. Both are guaranteed by the standard to signal successful completion. (It's barely possible that EXIT_SUCCESS could have a value other than 0, but it's equal to 0 on every implementation I've ever heard of.)

Using 0 has the minor advantage that you don't need #include <stdlib.h> in C, or #include <cstdlib> in C++ (if you're using a return statement rather than calling exit()) -- but for a program of any significant size you're going to be including stdlib directly or indirectly anyway.

For that matter, in C starting with the 1999 standard, and in all versions of C++, reaching the end of main() does an implicit return 0; anyway, so you might not need to use either 0 or EXIT_SUCCESS explicitly. (But at least in C, I consider an explicit return 0; to be better style.)

(Somebody asked about OpenVMS. I haven't used it in a long time, but as I recall odd status values generally denote success while even values denote failure. The C implementation maps 0 to 1, so that return 0; indicates successful termination. Other values are passed unchanged, so return 1; also indicates successful termination. EXIT_FAILURE would have a non-zero even value.)

Why return 0 from main() in a C program?

It returns the 0 to OS to tell the OS that your program executed successfully.

Proper way to return from main

As methods of terminating a program, return and exit are closely related. In fact, the statement

return EXIT_SUCCESS;

in main is equivalent to

exit (EXIT_SUCCESS);  

The difference between return and exit is that exit causes program termination regardless of which function calls it. The return statement causes program termination only when it appears in the main function.

Why does the main function work with no return value?

Normally it is not allowed for the control flow to reach the end of a non-void function without returning something. The main function is handled differently, as specified in the standard.

From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2960.pdf:

§ 3.6.1/5

If control reaches the end of main without encountering a return
statement, the effect is that of executing return 0;

As for the rationale, I'm not sure, honestly. If someone knows, please add it to my answer or as a comment.

What was the rationale for making `return 0` at the end of `main` optional?

In The New C Standard section 5.1.2.2.3 Program termination the author Derek Jones commentary on this lines from the C99 standard:

reaching the } that terminates the main function returns a value of 0

is:

The standard finally having to bow to sloppy existing practices.

Which indicates the rationale was to address poor programming practices with respect to explicitly returning a value from main. Prior to this the status returned was undefined.

He indicates that many implementations already implemented this even in C90, so the fact that this change already reflected common implementation also probably helped.

Memory waste? If main() should only return 0 or 1, why is main declared with int and not short int or even char?

1st: Alone your assumption/statement if it usually returns only 0 or 1 is wrong.

Usually the return code is expected to be 0 if no error occurred but otherwise it can return any number to represent different errors. And most (at least command line programs) do so. Many programs also output negative numbers.

However there are a few common used codes https://www.tldp.org/LDP/abs/html/exitcodes.html also here another SO member points to a unix header that contains some codes https://stackoverflow.com/a/24121322/2331592

So after all it is not just a C or C++ type thing but also has historical reasons how most operating systems work and expect the programs to behave and since that the languages have to support that and so at least C like languages do that by using an int main(...).

2nd:
your conclusion It is wasting memory space is wrong.

  1. Using an int in comparison to a shorter type does not involve any waste.
    Memory is usually handled in word-size (that that mean may depend from your architecture) anyway
  2. working with sub-word-types involves computation overheand on some architecture (read: load, word, mask out unrelated bits; store: load memory, mask out variable bits, or them with the new value, write the word back)
  3. the memory is not wasted unless you use it. if you write return 0; no memory is ever used at this point. if you return myMemorySaving8bitVar; you only have 1 byte used (most probable on the stack (if not optimized out at all))

Should I return 0 or 1 for successful function?

It's defined by the C standard as 0 for success (credits go to hvd).

But

For greater portability, you can use the macros EXIT_SUCCESS and
EXIT_FAILURE for the conventional status value for success and
failure, respectively. They are declared in the file stdlib.h.

(I'm talking about the value returned to the OS from main, exit or similar calls)

As for your function, return what you wish and makes code more readable, as long as you keep it that way along your programs.

Is returning zero from main necessary, and how can the return value from main be useful?

When writing scripts (like in Bash, or CMD.exe on Windows)
you can chain some commands with the && and || operators.

Canonically, a && b will run b if the result of a is zero, and a || b will run b if a returned nonzero.

This is useful if you wish to conditionally run a command if the previous one succeeded. For example, you would like to delete a file if it contains word foo. Then you will use :

grep foo myfile && rm myfile

grep returns 0 when there was a match, else nonzero.

Who receives the value 0 when we write return 0

Returning a value from the main() function is equivalent to calling exit() with that value. So your code is similar to:

int main() {
std::exit(0);
}

The exit status is returned to the runtime library. It can be accessed by the parent process that invoked the program; on Unix this is done using the wait() family of calls. If you run the program from a Unix shell, the exit status is put in the variable $?, or you can test it using control statements like if or while.



Related Topics



Leave a reply



Submit