Skip to content

Instantly share code, notes, and snippets.

@jazlopez
Last active December 31, 2023 21:11
Show Gist options
  • Save jazlopez/d676791592c80887da2195021be58610 to your computer and use it in GitHub Desktop.
Save jazlopez/d676791592c80887da2195021be58610 to your computer and use it in GitHub Desktop.
exit.status.code.in.c.program.md

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 return 0; has the minor advantage that you don't need these includes to call exit()

In C to use exit(EXIT_SUCCESS) you need:

#include <stdlib.h>

In C++ to use exit(EXIT_SUCCESS) you need:

#include <cstdlib>

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.)

https://stackoverflow.com/questions/8867871/should-i-return-exit-success-or-0-from-main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment