atoi_atoi_l、、 _wtoi_wtoi_l

文字列を整数に変換します。

構文

int atoi(
   const char *str
);
int _wtoi(
   const wchar_t *str
);
int _atoi_l(
   const char *str,
   _locale_t locale
);
int _wtoi_l(
   const wchar_t *str,
   _locale_t locale
);

パラメーター

str
変換対象の文字列。

locale
使用するロケール。

戻り値

各関数は、入力文字を数字として解釈して生成される int 値を返します。 戻り値は 0 で、_wtoi入力をその型の値に変換できない場合は 0 atoi です。

関数が大きな負の整数値でオーバーフローすると、 LONG_MIN 返されます。 atoi_wtoi は、これらの条件で INT_MAXINT_MIN を返します。 範囲外のすべての場合、errnoERANGE に設定されます。 渡されたパラメーターが指定されている場合、「NULLパラメーターの検証」で説明されているように、無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、これらの関数は errnoEINVAL に設定し、0 を返します。

解説

これらの関数は、文字列を整数値に変換します (atoi_wtoi)。 入力文字列は、指定された型の数値として解釈できる文字シーケンスです。 この関数は、数値の一部として認識できない最初の文字で入力文字列の読み取りを停止します。 この文字は、文字列を終了する null 文字 ('\0' または L'\0') である場合があります。

atoi および _wtoistr 引数には、次の形式があります。

[whitespace] [sign] [digits]

whitespace はスペースまたはタブで構成され、無視されます。sign はプラス (+) またはマイナス (-) のいずれかで、digits は 1 つ以上の数字です。

_l サフィックスが付いているこれらの関数の各バージョンは、現在のロケールの代わりに渡されたロケール パラメーターを使用する点を除いて同じです。 詳細については、「 Locale」を参照してください。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

汎用テキスト ルーチンのマップ

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tstoi atoi atoi _wtoi
_ttoi atoi atoi _wtoi

必要条件

ルーチン 必須ヘッダー
atoi <stdlib.h>
_atoi_l_wtoi_wtoi_l <stdlib.h> または <wchar.h>

このプログラムは、atoi 関数を使用して、文字列として格納されている数字を数値に変換する方法を示します。

// crt_atoi.c
// This program shows how numbers
// stored as strings can be converted to
// numeric values using the atoi functions.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
    char    *str = NULL;
    int     value = 0;

    // An example of the atoi function.
    str = "  -2309 ";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

    // Another example of the atoi function.
    str = "31412764";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );

    // Another example of the atoi function
    // with an overflow condition occurring.
    str = "3336402735171707160320";
    value = atoi( str );
    printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
    if (errno == ERANGE)
    {
       printf("Overflow condition occurred.\n");
    }
}
Function: atoi( "  -2309 " ) = -2309
Function: atoi( "31412764" ) = 31412764
Function: atoi( "3336402735171707160320" ) = 2147483647
Overflow condition occurred.

関連項目

データ変換
数学と浮動小数点のサポート
ロケール
_ecvt
_fcvt
_gcvt
setlocale, _wsetlocale
_atodbl_atodbl_l, _atoldbl, _atoldbl_l, _atoflt_atoflt_l