Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

The ISO 1998 c++ standard specifies that not explicitly using a return statement in the main is equivalent to use return 0. But what if an implementation has a different standard "no error" code, for example -1?

Why not use the standard macro EXIT_SUCCESS that would be replaced either by 0 or -1 or any other value depending on the implementation?

C++ seems to force the semantic of the program, which is not the role of a language which should only describe how the program behaves. Moreover the situation is different for the "error" return value: only EXIT_FAILURE is a standard "error" termination flag, with no explicit value, like "1" for example.

What are the reasons of these choices?

like image 577
Pragmateek Avatar asked Jul 27 '09 14:07

Pragmateek


People also ask

What does return EXIT_SUCCESS mean?

The function printf is defined in stdio. h. The next statement, return EXIT_SUCCESS; indicates that when the main function exits, it should return the value EXIT_SUCCESS. The value EXIT_SUCCESS is defined in stdlib.

Do you have to return 0 in main C?

As the user-defined function declaration mandates return 0, so we must utilize return 0, or return -1 within each C program. If we wouldn't directly declare a value, the assembler automatically includes a return 0; so it is optional to insert a return 0.

Why do we write return 0 in C program?

C++ return 0 in the main function means that the program executed successfully. return 1 in the main function means that the program does not execute successfully and there is some error.

What is the default return value?

The default return value from a function is int. Unless explicitly specified the default return value by compiler would be integer value from function.


2 Answers

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.

like image 196
Michael Burr Avatar answered Oct 12 '22 19:10

Michael Burr


Actually, return 0 won't necessarily return 0! I'm quoting the C standard here, because it's what I know best.

About return in main():

5.1.2.2.3 Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;

About exit():

7.20.4.3 The exit function
Synopsis

#include <stdlib.h> void exit(int status); 

[...]
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.

like image 28
Bastien Léonard Avatar answered Oct 12 '22 17:10

Bastien Léonard