YouTip LogoYouTip

C Function Strerror

## C Library Function - `strerror()` The `strerror()` function is a built-in utility in the C standard library that maps an error number (typically stored in the global variable `errno`) to a human-readable, system-defined error message string. This function is highly useful for debugging and logging, as it translates abstract error codes into meaningful descriptions that help developers and users understand why a system call or library function failed. --- ## Header File To use the `strerror()` function, you must include the `` header file in your C program: ```c #include ``` --- ## Syntax & Declaration ```c char *strerror(int errnum); ``` ### Parameters * **`errnum`**: The error number to be translated. This is typically the global error variable `errno` (defined in ``), which is set by system calls and some library functions when an error occurs. However, you can pass any valid integer error code. ### Return Value * The function returns a pointer to a null-terminated string containing the system-defined error message corresponding to `errnum`. * The returned string must not be modified by the program. * The exact text of the error messages depends on the operating system, platform, and compiler implementation. --- ## Code Example The following example demonstrates how to use `strerror()` to print a descriptive error message when attempting to open a non-existent file. ```c #include #include #include int main() { FILE *fp; // Attempt to open a file that does not exist in read-only mode fp = fopen("non_existent_file.txt", "r"); if (fp == NULL) { // Print the human-readable error message corresponding to the current errno printf("Error opening file: %s\n", strerror(errno)); } else { fclose(fp); } return 0; } ``` ### Output If you compile and run this program on a system where `non_existent_file.txt` does not exist, it will produce the following output: ```text Error opening file: No such file or directory ``` --- ## Important Considerations ### 1. Thread Safety The standard `strerror()` function is **not guaranteed to be thread-safe**. Many implementations return a pointer to a static buffer that is overwritten on subsequent calls to `strerror()`. If you are working in a multi-threaded environment, consider using the thread-safe alternatives: * **`strerror_r()`** (POSIX standard): ```c int strerror_r(int errnum, char *buf, size_t buflen); ``` * **`strerror_s()`** (C11 standard Annex K): ```c errno_t strerror_s(char *buf, rsize_t buflen, errno_t errnum); ``` ### 2. Read-Only Return Value The pointer returned by `strerror()` points to a string that should be treated as read-only. Modifying this string directly can lead to undefined behavior. ### 3. Platform Dependency While standard error codes like `ENOENT` (No such file or directory) or `EACCES` (Permission denied) are common across POSIX-compliant systems, the exact wording of the error messages returned by `strerror()` may vary slightly between operating systems (e.g., Linux, macOS, and Windows).
← C Function StrlenC Function Strcspn β†’