Share via


atol、_atol_l、_wtol、_wtol_l

更新 : 2007 年 11 月

文字列を長整数型 (long) に変換します。

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 型の値を返します。入力値をその型の値に変換できない場合、atol の戻り値は 0L です。

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

解説

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

入力文字列は、指定された型の数値として解釈できる文字の並びにします。関数は、入力文字列の読み込み時に、数値の一部として認識できない文字が現れた時点で停止します。この文字は、文字列の終わりを示す NULL ('\0' または L'\0') の場合もあります。

atol の str 引数は、次の形式です。

[whitespace] [sign] [digits]]

whitespace はスペースやタブで構成され、無視されます。sign は正符号 (+) または負符号 (–) のいずれかで、digits は 1 つ以上の 10 進数字です。

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

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

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

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.

.NET Framework の相当するアイテム

参照

参照

データ変換

浮動小数点サポート

ロケール

_ecvt

_fcvt

_gcvt

setlocale、_wsetlocale

_atodbl、_atodbl_l、_atoldbl、_atoldbl_l、_atoflt _atoflt_l