C programming string.h function - char *strerror(int errnum)

The strerror function in the string.h library returns a string that describes the error code passed to it as a parameter.

Here's the syntax:

refer‮t ‬o:theitroad.com
char *strerror(int errnum);

The errnum parameter is an integer error code. The function returns a pointer to a string that describes the error. The returned string may be overwritten by subsequent calls to strerror.

Here's an example usage:

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
    int fd = open("nonexistent_file.txt", 0);
    if (fd == -1) {
        fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
    }
    return 0;
}

In this example, strerror is used to get a string that describes the error code returned by errno. The error string is printed to stderr using fprintf.