Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Thursday, June 13, 2019
 
 

strerror(3C)

Name

strerror, strerror_l, strerror_r - get error message string

Synopsis

#include <string.h>

char *strerror(int errnum);
char *strerror_l(int errnum, locale_t locale);
int strerror_r(int errnum, char *strerrbuf, size_t  buflen);

Description

The strerror() function maps the error number in errnum to a locale-dependent error message string determined by the setting of the LC_MESSAGES category in the current locale, and returns a pointer to that string. It uses the same set of error messages as perror(3C).

The application shall not modify the string returned. The returned string pointer might be invalidated or the string content might be overwritten by a subsequent call to strerror(), or by a subsequent call to strerror_l() in the same thread.

Since no return value is reserved to indicate an error of strerror(), an application wishing to check for error situations should set errno to 0, then call strerror(), then check errno.

The strerror_l() function maps the error number in errnum to a locale-dependent error message string in the locale represented by locale, and returns a pointer to that string. It uses the same set of error messages as perror(3C). The returned string should not be overwritten.

Since strerror_l() is required to return a string for some errors, an application wishing to check for all error situations should set errno to 0, then call strerror_l(), then check errno.

The behavior is undefined if the <locale> argument to strerror_l() is the special locale object LC_GLOBAL_LOCALE or is not a valid locale object handle.

The strerror_r() function maps the error number in errnum to an error message string and returns the string in the buffer pointed to by strerrbuf with length buflen.

Return Values

Upon successful completion, strerror() and strerror_l() return a pointer to the generated message string. Otherwise, it sets errno and returns a pointer to an error message string. It returns the string “Unknown error” if errnum is not a valid error number.

Upon successful completion, strerror_r() returns 0. Otherwise it sets errno and returns the value of errno to indicate the error. It returns the string “Unknown error” in the buffer pointed to by strerrbuf if errnum is not a valid error number.

Errors

These functions may fail if:

EINVAL

The value of errnum is not a valid error number.

The strerror_r() function may fail if:

ERANGE

The buflen argument specifies insufficient storage to contain the generated message string.

Usage

Messages returned from these functions are in the native language specified by the LC_MESSAGES locale category. See setlocale(3C).

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Safe
Standard

See Also

duplocale(3C), freelocale(3C), gettext(3C), newlocale(3C), perror(3C), setlocale(3C), uselocale(3C), attributes(7), standards(7)