10.5.6 C標準ライブラリ関数のエラーメッセージ

ライブラリ関数の中には、ライブラリ関数を実行中にエラーが発生した場合、標準ライブラリのヘッダファイル<errno.h>で定義しているマクロerrno にエラー番号を設定するものがあります。

エラー番号には対応するエラーメッセージが定義してあり、エラーメッセージを出力することができます。エラーメッセージを出力するプログラム例を以下に示します。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
 
main()
{
    FILE *fp;
 
    fp=fopen("file", "w");
    fp=NULL;
 
    fclose(fp);                       /* error occurred */
 
    printf("%s\n", strerror(errno));  /* print error message */
}

 

説明

(1)

fclose関数に値NULLのファイルポインタを実引数として渡しているので、エラーとなります。このときerrnoに対応するエラー番号が設定されます。

(2)

strerror関数は、エラー番号を実引数として渡すと、対応するエラーメッセージの文字列のポインタを返します。printf関数の文字列出力指定によりエラーメッセージを出力します。

 

標準ライブラリエラーメッセージ一覧

エラー番号

エラーメッセージ/説明

エラー番号を設定する関数

0x22

(ERANGE)

Data out of range

オーバーフローが発生しました。

frexp, ldexp, modf, ceil, floor, fmod, atof, atoi, atol, atoll, atolfixed, atolaccum, strtod, strtol, strtoul, strtoll, strtoull, strtolfixed, strtolaccum, perror, fprintf, fscanf, printf, scanf, sprintf, sscanf, vfprintf, vprintf, vsprintf, acos, acosf, asin, asinf, atan, atan2, atan2f, atanf, ceilf, cos, cosf, cosh, coshf, exp, expf, floorf, fmodf, ldexpf, log,log10, log10f, logf, modff, pow, powf, sin, sinf, sinh, sinhf, sqrt, sqrtf, tan, tanf, tanh, tanhf, fabs, fabsf, frexpf

0x21

(EDOM)

Data out of domain

数学関数の引数に対する結果の値が定義されません。

acos, acosf, asin, asinf, atan, atan2, atan2f, atanf, ceil, ceilf, cos, cosf, cosh, coshf, exp, expf, floor, floorf, fmod, fmodf, ldexp, ldexpf, log, log10, log10f, logf, modf, modff, pow, powf, sin, sinf, sinh, sinhf, sqrt, sqrtf, tan, tanf, tanh, tanhf, fabs, fabsf, frexp, frexpf

0x450

(ESTRN)

Too long string

文字列の文字数が512文字を超えています。

atof, atoi, atol, atoll, atolfixed, atolaccum, strtod, strtol, strtoul, strtoll, strtoull, strtolfixed, strtolaccum

0x04B0

(ECBASE)

Invalid radix

基数の指定が誤っています。

strtol, strtoul, strtoll, strtoull

0x04B2

(ETLN)

Number too long

数値を表現する文字列の文字数が有効桁数を超えています。

atof, atolfixed, atolaccum, strtod, strtolfixed, strtolaccum, fscanf, scanf, sscanf

0x04B4

(EEXP)

Exponent too large

指数部の桁数が3桁を超えています。

atof, strtod, fscanf, scanf, sscanf

0x04B6

(EEXPN)

Normalized exponent too large

文字列を一度IEEE規格の10進形式に正規化したとき指数部の桁数が3桁を超えています。

atof, strtod, fscanf, scanf, sscanf

0x04BA

(EFLOATO)

Overflow out of float

float型の10進数値が、float型の範囲を超えています(オーバーフロー)。

fscanf, scanf, sscanf

0x04C4

(EFLOATU)

Underflow out of float

float型の10進数値が、float型の範囲を超えています(アンダーフロー)。

fscanf, scanf, sscanf

0x04E2

(EDBLO)

Overflow out of double

double型の10進数値が、double 型の範囲を超えています(オーバーフロー)。

fscanf, scanf, sscanf

0x04EC

(EDBLU)

Underflow out of double

double型の10進数値が、double型の範囲を超えています(アンダーフロー)。

fscanf, scanf, sscanf

0x04F6

(ELDBLO)

Overflow out of long double

long double型の10進数値が、long double型の範囲を超えています(オーバーフロー)。

fscanf, scanf, sscanf

0x0500

(ELDBLU)

Underflow out of long double

long double型の10進数値が、long double型の範囲を超えています(アンダーフロー)。

fscanf, scanf, sscanf