Namespaces
Variants

strtol, strtoll

ja.cppreference.net より
ヘッダ <stdlib.h>
long      strtol( const char*          str, char**          str_end, int base );
で定義 (1) (C99 まで)
long      strtol( const char* restrict str, char** restrict str_end, int base );
(C99 以降)
long long strtoll( const char* restrict str, char** restrict str_end, int base );
(2) (C99 以降)

str が指すバイト文字列内の整数値を解釈します。 str

最初の非空白文字が見つかるまで空白文字 (isspace() で識別される) をすべて破棄し、その後、有効な base-n (n=base) 整数表現を形成できる限り多くの文字を取り込んで整数値に変換します。有効な整数値は以下の部分から構成されます: isspace base-n (ここで n=base) 整数表現

  • (オプション) プラスまたはマイナス記号
  • (オプション) 接頭辞 (0) 8進数を示す (base が 8 または 0 の場合のみ適用)
  • (オプション) 接頭辞 (0x または 0X) 16進数を示す (base が 16 または 0 の場合のみ適用)
  • 数字の並び

base に有効な値の範囲は {0, 2, 3, ..., 36} です。base-2 整数に有効な数字は {0, 1}、base-3 整数に有効な数字は {0, 1, 2}、などです。10 より大きい基数の場合、有効な数字にはアルファベット文字が含まれ、base-Aa 整数の 11 から base-Zz 整数の 36 までとなります。文字の大文字小文字は区別されません。

現在インストールされている C ロケール によって追加の数値形式が受け入れられる場合があります。

base の値が base の場合、数値の基数は自動検出されます: 接頭辞が 0 なら基数は8進数、接頭辞が 0 または 0x なら基数は16進数、それ以外なら基数は10進数です。 0Xマイナス記号が入力シーケンスの一部であった場合、数字の並びから計算された数値は、結果型で単項マイナスによって否定されたかのように扱われます。

単項マイナス

この関数は、str_end が指すポインタを、解釈された最後の数字の次の文字を指すように設定します。str_end がヌルポインタの場合は無視されます。 str_endstr_endstr_endstr_end

str が空であるか期待される形式でない場合、変換は行われず、(str_end がヌルポインタでなければ) str の値が str_end が指すオブジェクトに格納されます。 strstrstr_endstr_endstrstrstr_endstr_end

引数

str - 解釈されるナル終端バイト文字列へのポインタ
str_end - 文字へのポインタへのポインタ
base - 解釈される整数値の基数

戻り値

  • 成功した場合、str の内容に対応する整数値が返されます。strstr
  • 変換された値が対応する戻り値の型の範囲外の場合、範囲エラーが発生し (errno に ERANGE が設定され)、値の符号と戻り値の型に応じて LONG_MAX、LONG_MIN、LLONG_MAX、または LLONG_MIN が返されます。errno に ERANGE が設定されLONG_MAX、LONG_MIN、LLONG_MAX、または LLONG_MIN が返されます。
  • 変換が一切行われなかった場合、0 が返されます。

例

#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    // parsing with error handling
    const char* p = "10 200000000000000000000000000000 30 -40 junk";
    printf("Parsing '%s':\n", p);

    for (;;)
    {
        // errno can be set to any non-zero value by a library function call
        // regardless of whether there was an error, so it needs to be cleared
        // in order to check the error set by strtol
        errno = 0;
        char* end;
        const long i = strtol(p, &end, 10);
        if (p == end)
            break;
 
        const bool range_error = errno == ERANGE;
        printf("Extracted '%.*s', strtol returned %ld.", (int)(end-p), p, i);
        p = end;
 
        if (range_error)
            printf("\n --> Range error occurred.");
 
        putchar('\n');
    }
    
    printf("Unextracted leftover: '%s'\n\n", p);
 
    // parsing without error handling
    printf("\"1010\" in binary  --> %ld\n", strtol("1010", NULL, 2));
    printf("\"12\"   in octal   --> %ld\n", strtol("12",   NULL, 8));
    printf("\"A\"    in hex     --> %ld\n", strtol("A",    NULL, 16));
    printf("\"junk\" in base-36 --> %ld\n", strtol("junk", NULL, 36));
    printf("\"012\"  in auto-detected base --> %ld\n", strtol("012",  NULL, 0));
    printf("\"0xA\"  in auto-detected base --> %ld\n", strtol("0xA",  NULL, 0));
    printf("\"junk\" in auto-detected base --> %ld\n", strtol("junk", NULL, 0));
}

出力例:

Parsing '10 200000000000000000000000000000 30 -40 junk':
Extracted '10', strtol returned 10.
Extracted ' 200000000000000000000000000000', strtol returned 9223372036854775807.
 --> Range error occurred.
Extracted ' 30', strtol returned 30.
Extracted ' -40', strtol returned -40.
Unextracted leftover: ' junk'

"1010" in binary  --> 10
"12"   in octal   --> 10
"A"    in hex     --> 10
"junk" in base-36 --> 926192
"012"  in auto-detected base --> 10
"0xA"  in auto-detected base --> 10
"junk" in auto-detected base --> 0

参考文献

  • C23 規格 (ISO/IEC 9899:2024):
  • 7.24.1.7 The strtol, strtoll, strtoul, and strtoull functions (p. 362-363)
  • C17 規格 (ISO/IEC 9899:2018):
  • 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p. 251-252)
  • C11 規格 (ISO/IEC 9899:2011):
  • 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p. 344-345)
  • C99 規格 (ISO/IEC 9899:1999):
  • 7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions (p. 310-311)
  • C89/C90 規格 (ISO/IEC 9899:1990):
  • 4.10.1.5 The strtol function

関連項目

バイト文字列を整数値に変換する
(関数)
バイト文字列を符号なし整数値に変換する
(関数)
(C95)(C99)
ワイド文字列を整数値に変換する
(関数)
ワイド文字列を符号なし整数値に変換する
(関数)
C++ ドキュメント for strtol, strtoll