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.
Is there a standard set of exit code numbers for C/C++?
683 views Asked by AudioBubble AtThere are 3 answers
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.
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.
Program exit codes
Short answer: no, except for
EXIT_SUCCESSandEXIT_FAILURE, which are defined instdlib.h. See here: https://en.cppreference.com/w/c/program/EXIT_status.From the community wiki link above:
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.
errnoerror numbers assigned to theerrnoglobal variable when a function call fails or has an errorAlso no, but most systems will provide a "standardized" set of error numbers for that system.
But,
errnoerror 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:
I typically print the
errnovalue when a function call fails so that the code author or user can debug the run-time crash. Printing theerrnovalue, and a human-readable description of it viastrerror(errno), might be done like this: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: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-2001orPOSIX.1-2008), or part of theC99standard.The actual error numbers can be tracked down via the
errno.hheader file, which may include other files. Example: here are some of the error number definitions on Linux: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.hWhich 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.hSee also
strerror(errno)provides a description string of a given error code/number. See here: https://en.cppreference.com/w/c/string/byte/strerror