strerror() — ランタイム・エラー・メッセージを指すポインターの設定

フォーマット

#include <string.h>
char *strerror(int errnum);

言語レベル

ANSI

スレッド・セーフ

はい

説明

strerror() 関数は、errnum のエラー番号を、 エラー・メッセージ・ストリングにマップします。

戻り値

strerror() 関数は、ストリングを指すポインターを戻します。 NULL 値は戻しません。 errno の値は ECONVERT (変換エラー) に設定される可能性があります。

この例では、ファイルをオープンし、エラーが発生した場合にランタイム・エラー・メッセージを戻します。
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
int main(void)
{
   FILE *stream;
 
   if ((stream = fopen("mylib/myfile", "r")) == NULL)
      printf(" %s ¥n", strerror(errno));
 
}
 
/*  This is a program fragment and not a complete function example  */