atol, _atol_l, _wtol, _wtol_l

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

構文

long atol(
   const char *str
);
long _atol_l(
   const char *str,
   _locale_t locale
);
long _wtol(
   const wchar_t *str
);
long _wtol_l(
   const wchar_t *str,
   _locale_t locale
);

パラメーター

str
変換対象の文字列。

locale
使用するロケール。

戻り値

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

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

解説

これらの関数は、文字列を長整数値 (atol) に変換します。

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

stratol 引数には、次の形式があります。

[whitespace] [sign] [digits]

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

_wtol は、ワイド文字列を受け取る点を除いて atol と同じです。

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

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

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

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tstol atol atol _wtol
_ttol atol atol _wtol

必要条件

ルーチン 必須ヘッダー
atol <stdlib.h>
_atol_l, _wtol, _wtol_l <stdlib.h> および <wchar.h>

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

// crt_atol.c
// This program shows how numbers stored as
// strings can be converted to numeric values
// using the atol functions.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

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

    // An example of the atol function
    // with leading and trailing white spaces.
    str = "  -2309 ";
    value = atol( str );
    printf( "Function: atol( \"%s\" ) = %d\n", str, value );

    // Another example of the atol function
    // with an arbitrary decimal point.
    str = "314127.64";
    value = atol( str );
    printf( "Function: atol( \"%s\" ) = %d\n", str, value );

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

関連項目

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