Exit Codes
An exit code is the integer a process reports to its parent when it terminates. Zero means success. Any other value means failure, with the exact meaning left to the program.
Detailed Explanation
When a hosted C program returns from main or calls exit, the C library turns that int into a process status the operating system can wait on. Shells store it in $?. make treats a non-zero status as a failed recipe. Cron, CI, and supervisors all branch on it. The number is a contract with whoever started you.
ISO C defines two portable values as macros in <stdlib.h>. EXIT_SUCCESS is success and equals 0 on every common platform. EXIT_FAILURE is failure; it is 1 on Unix-like systems and can be a different small integer elsewhere. Returning a bare 0 is portable. Returning a bare 1 is almost always what you want on Unix and is not strictly guaranteed by the C standard.
Unix widens the convention. The kernel keeps only the low eight bits of the status, so values from 0 to 255 are what the parent actually sees. waitpid overlays extra bits for signals: if the process died from SIGSEGV, the parent sees a signal death, not 139 as an exit value, though shells print 139 as a convenience (128 + 11). Codes 128 and above are therefore a crowded neighborhood. Pick small, documented numbers for your own failures.
exit(status) and return status from main are equivalent for the purpose of the code the parent sees. They are not equivalent for control flow: exit unwinds nothing in your function, but it does run atexit handlers and flush stdio. _Exit and _exit skip those handlers. abort raises SIGABRT and typically yields a signal death rather than a clean exit code.
A program that writes an error to stdout and returns 0 has lied. A program that prints nothing and returns 1 has told the truth in the only channel a script can test. Put diagnostics on stderr, put data on stdout, and put the outcome in the exit code.
Code Examples
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s FILE\n", argv[0]);
return EXIT_FAILURE;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
perror(argv[1]); /* writes to stderr */
return EXIT_FAILURE;
}
int ch = fgetc(fp);
if (fclose(fp) == EOF) {
perror("fclose");
return EXIT_FAILURE;
}
if (ch == EOF) {
return EXIT_FAILURE; /* empty or unreadable */
}
putchar(ch);
return EXIT_SUCCESS;
}#include <stdlib.h>
/* Distinct codes for a caller that wants to branch. Document them.
Unix parents see only status & 0xff, so stay in 1..127. */
enum {
APP_OK = 0,
APP_USAGE = 2,
APP_IO = 3,
APP_NO_MEMORY = 4
};
int main(void)
{
char *buf = malloc(64);
if (buf == NULL) {
return APP_NO_MEMORY;
}
free(buf);
return APP_OK;
}Common Gotchas
Only the low eight bits survive on Unix
`exit(256)` becomes `0` in the parent, which looks like success. Stay in `0..127` for codes you invent.
`EXIT_FAILURE` is portable; a bare `1` is a Unix habit
both work on Linux and macOS. Prefer the macro when the file already includes `<stdlib.h>`.
`abort` is not `exit(1)`
the parent sees a signal, stdio may not flush, and `atexit` handlers do not run.
Returning `0` after a failed write hides the bug from scripts
check `ferror`, `fclose`, and `fflush` before claiming success.