3

Say we have the program:

int main(int argc, char** argv){
  if(argc < 3){
    printf("usage details\n");
    return EXIT_FAILURE;
  }
  dostuff();
  return EXIT_SUCCESS;
}

I've seen this as well:

int main(int argc, char** argv){
  if(argc < 3){
    printf("usage details\n");
    exit(EXIT_FAILURE);
  }
  dostuff();
  exit(EXIT_SUCCESS);
}

According to ISO/IEC 9899:1989 (C90):

The standard defines 3 values for returning that are strictly conforming (that is, does not rely on implementation defined behaviour): 0 and EXIT_SUCCESS for a successful termination, and EXIT_FAILURE for an unsuccessful termination. Any other values are non-standard and implementation defined. main must have an explicit return statement at the end to avoid undefined behaviour.

Also, according to ISO/IEC 9899:2011:

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; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

It states that the return from main is equivalent to calling exit with the same return value.

That being said, the question is: Does it matter, whether you return from main and have it call exit for you, or exit from main

user3386109
  • 34,287
  • 7
  • 49
  • 68
Scotty Bauer
  • 1,277
  • 9
  • 14
  • It's equivalent. As the standard says. i.e. interchangeable. – Mat Nov 11 '13 at 18:24
  • 2
    Depends. Are you ever planning to call main() yourself in your own code (perhaps recursively)? If so, exit() is a limiting choice. – John Deters Nov 11 '13 at 18:27
  • 4
    @JohnDeters: The standard also prohibits calling `main()`. –  Nov 11 '13 at 18:31
  • 2
    @duskwuff, that's the C++ standard. AFAIK, it's legal in C. That still doesn't make it a particularly good idea, tho. – John Deters Nov 11 '13 at 18:38
  • Yes, calling main is legal in C, but doesn't seems a good idea (especially if we define at_exit) – David Ranieri Nov 11 '13 at 18:50
  • You will find a different answer for different versions of the C standard. See [this answer](https://stackoverflow.com/a/207992/2472827). – Neil Sep 24 '21 at 21:39

1 Answers1

3

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.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    Another difference is that `exit` can be used in constructions like `a == 1 ? do_stuff() : exit(EXIT_FAILURE);` and `return` can not – David Ranieri Nov 11 '13 at 18:41