c++cexitexit-code

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


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.


Solution

  • 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:
    void some_func()
    {
        // ...
    
        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