Is there a standard set of exit code numbers for C/C++?

683 views Asked by At

AFAIK, the exit code numbers may vary depending on the application and the conventions adopted in a project. However, I wonder if there is an exit code standard list for C/C++ projects.

3

There are 3 answers

2
Gabriel Staples On BEST ANSWER

Program exit codes

Short answer: no, except for EXIT_SUCCESS and EXIT_FAILURE, which are defined in stdlib.h. See here: https://en.cppreference.com/w/c/program/EXIT_status.

From the community wiki link above:

Notes

Both EXIT_SUCCESS and the value zero indicate successful program execution status (see exit), although it is not required that EXIT_SUCCESS equals zero.

Function return codes

Also no.

But, most functions have a description which can be looked up on https://en.cppreference.com/w/ (a rather thorough community wiki) or https://cplusplus.com/ (also a community wiki) to help you know what the return values should be.

errno error numbers assigned to the errno global variable when a function call fails or has an error

Also no, but most systems will provide a "standardized" set of error numbers for that system.

But, errno error numbers are intended to be used by the caller of the function which returned the error. They are not intended to be returned as error codes when a program crashes.

From @John Bollinger's comment:

Error numbers are values that are communicated to the caller of the function that encountered the error, via the errno variable or as the function return value, depending on the function. Neither are they meant for use as program exit statuses nor is there any convention of using them that way.

I typically print the errno value when a function call fails so that the code author or user can debug the run-time crash. Printing the errno value, and a human-readable description of it via strerror(errno), might be done like this:

#include <errno.h>   // `errno`
#include <string.h>  // `strerror(errno)`

// ...

// in some function:

int retcode = clock_gettime(CLOCK_REALTIME, &ts);
if (retcode == -1)
{
    printf("Failed to get a timestamp. errno = %i: %s\n",
        errno, strerror(errno));
}

You can see that usage in my program here, for instance: timing_clock_gettime_full_demo.c in my eRCaGuy_hello_world repo.

Examples of where errnos are defined, for various systems:

  1. For Linux: https://man7.org/linux/man-pages/man3/errno.3.html:

    Notice that in these error names, they also indicate if it is part of the POSIX standard (POSIX.1-2001 or POSIX.1-2008), or part of the C99 standard.

    The actual error numbers can be tracked down via the errno.h header file, which may include other files. Example: here are some of the error number definitions on Linux:

    1. https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/errno.h, which includes:
    2. https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/errno-base.h
  2. For Microchip PIC32M microcontrollers:

    You can see the error codes in <errno.h> here: https://github.com/ElectricRCAircraftGuy/Microchip_XC32_Compiler/blob/main/xc32-v4.35-src/pic32m-source/newlib/newlib/libc/include/errno.h

    Which includes <sys/errno.h> here, where the error numbers are actually defined: https://github.com/ElectricRCAircraftGuy/Microchip_XC32_Compiler/blob/main/xc32-v4.35-src/pic32m-source/newlib/newlib/libc/include/sys/errno.h

See also

  1. strerror(errno) provides a description string of a given error code/number. See here: https://en.cppreference.com/w/c/string/byte/strerror
3
Jerry Coffin On

A far as C++ itself goes, there are only three values, specifying two conditions: 0, EXIT_FAILURE, and EXIT_SUCCESS.

0 and EXIT_SUCCESS both give implementation defined indications of successful exit. They're both described the same way in the standard, but there's no guarantee that they'll actually be the same value (though they are on most typical platforms).

EXIT_FAILURE gives an implementation defined indication of failure.

Anything else is up to the platform. Most of the common platforms like Linux, Windows, MacOS and *BSD will preserve at least the 8 least significant bits being returned to the shell.

0
John Bollinger On

AFAIK, the exit code numbers may vary depending on the application and the conventions adopted in a project.

And, importantly, on the conventions of the host system.

However, I wonder if there is a exit code standard list for C/C++ projects.

C itself takes care to avoid declaring any such thing, except that exit status 0 communicates success. Other than that, it specifies macros EXIT_SUCCESS and EXIT_FAILURE, whose (unspecified) values communicate program success and program failure, respectively.

The POSIX specifications for the shell command language do specify a few exit statuses for shell commands. That would fall into the category of host system conventions, and at least on POSIX systems, it is wise to avoid exit statuses that conflict with those.