strerror

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

strerror, strerrorname_np, strerrordesc_np, strerror_r, strerror_l - エラー番号を説明する文字列を返す。  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <string.h>

char *strerror(int errnum);
const char *strerrorname_np(int errnum);
const char *strerrordesc_np(int errnum);

int strerror_r(size_t size;
               int errnum, char buf[size], size_t size);
               /* XSI-compliant */

char *strerror_r(size_t size;
               int errnum, char buf[size], size_t size);
               /* GNU-specific */

char *strerror_l(int errnum, locale_t locale);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

strerrorname_np(), strerrordesc_np():

    _GNU_SOURCE

strerror_r():

    The XSI-compliant version is provided if:
        (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
    Otherwise, the GNU-specific version is provided.
 

説明

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language. (For example, if errnum is EINVAL, the returned description will be "Invalid argument".) This string must not be modified by the application, and the returned pointer will be invalidated on a subsequent call to strerror() or strerror_l(), or if the thread that obtained the string exits. No other library function, including perror(3), will modify this string.

Like strerror(), the strerrordesc_np() function returns a pointer to a string that describes the error code passed in the argument errnum, with the difference that the returned string is not translated according to the current locale.

The strerrorname_np() function returns a pointer to a string containing the name of the error code passed in the argument errnum. For example, given EPERM as an argument, this function returns a pointer to the string "EPERM". Given 0 as an argument, this function returns a pointer to the string "0".  

strerror_r()

strerror_r() is like strerror(), but might use the supplied buffer buf instead of allocating one internally. This function is available in two versions: an XSI-compliant version specified in POSIX.1-2001 (available since glibc 2.3.4, but not POSIX-compliant until glibc 2.13), and a GNU-specific version (available since glibc 2.0). The XSI-compliant version is provided with the feature test macros settings shown in the SYNOPSIS; otherwise the GNU-specific version is provided. If no feature test macros are explicitly defined, then (since glibc 2.4) _POSIX_C_SOURCE is defined by default with the value 200112L, so that the XSI-compliant version of strerror_r() is provided by default.

The XSI-compliant strerror_r() is preferred for portable applications. It returns the error string in the user-supplied buffer buf of size size.

The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most size bytes are stored (the string may be truncated if size is too small and errnum is unknown). The string always includes a terminating null byte ('\0').  

strerror_l()

strerror_l() は strerror() と同様だが、 errnum を locale で指定されたロケールのロケール依存のエラーメッセージにマッピングする。 locale が特別なロケールオブジェクト LC_GLOBAL_LOCALE の場合、もしくは locale が有効なロケールオブジェクトハンドルでない場合は、 strerror_l() の動作は未定義である。  

返り値

関数 strerror(), strerror_l() と GNU 固有の関数 strerror_r() はエラー内容を説明する文字列を返す。 エラー番号が未知の場合は "Unknown error nnn" という メッセージを返す。

On success, strerrorname_np() and strerrordesc_np() return the appropriate error description string. If errnum is an invalid error number, these functions return NULL.

The XSI-compliant strerror_r() function returns 0 on success. On error, a (positive) error number is returned (since glibc 2.13), or -1 is returned and errno is set to indicate the error (before glibc 2.13).

POSIX.1-2001 と POSIX.1-2008 では、 strerror() や strerror_l() が成功した場合は errno を変更せずに元のままにしなければならないとされている。関数のどの返り値もエラーを示すために予約されていないので、エラーをチェックしたいアプリケーションは呼び出しを行う前に errno を 0 に初期化し、呼び出しの後で errno をチェックすべき点に注意すること。  

エラー

EINVAL
errnum の値が有効なエラー番号ではない。
ERANGE
エラーコードを説明する文字列のために、充分な領域が確保できなかった。
 

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strerror() Thread safety MT-Safe
strerrorname_np(), strerrordesc_np() Thread safetyMT-Safe
strerror_r(), strerror_l() Thread safetyMT-Safe

Before glibc 2.32, strerror() is not MT-Safe.  

標準規格

strerror()
C11, POSIX.1-2008.
strerror_r()
strerror_l()
POSIX.1-2008.
strerrorname_np()
strerrordesc_np()
GNU.

POSIX.1-2001 は、 strerror() がエラーに遭遇した場合に errno をセッ トすることを認めているが、エラー発生時に関数の結果として どんな値を返す べきかを規定してない。 あるシステムでは、 エラー番号が未知の場合、 strerror() は NULL を返す。 他のシステムでは、 エラー番号が未知の場 合、 strerror() は "Error nnn occurred" といった文字列を返し、 errno に EINVAL をセットする。 C99 と POSIX.1-2008 では、返り値が NULL 以外になることが求められている。  

歴史

strerror()
POSIX.1-2001, C89.
strerror_r()
POSIX.1-2001.
strerror_l()
glibc 2.6. POSIX.1-2008.
strerrorname_np()
strerrordesc_np()
glibc 2.32.
 

注意

strerrorname_np() and strerrordesc_np() are thread-safe and async-signal-safe.  

関連項目

err(3), errno(3), error(3), perror(3), strsignal(3), locale(7), signal-safety(7)


 

Index

名前
ライブラリ
書式
説明
strerror_r()
strerror_l()
返り値
エラー
属性
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.