名稱空間
變體
操作

strtol, strtoll

來自 cppreference.com
< c‎ | string‎ | byte
在標頭檔案 <stdlib.h> 中定義
long      strtol( const char*          str, char**          str_end, int base );
(直到 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 );
(C99 起)

str 指向的位元組字串解釋為整數值。

丟棄任何空白字元(透過呼叫 isspace 識別),直到找到第一個非空白字元,然後儘可能多地獲取字元以形成有效的 *n 進位制*(其中 n=base)整數表示,並將其轉換為整數值。有效的整數值包含以下部分

  • (可選) 加號或減號
  • (可選) 字首 (0) 指示八進位制基數(僅當基數為 80 時適用)
  • (可選) 字首 (0x0X) 指示十六進位制基數(僅當基數為 160 時適用)
  • 一系列數字

base 的有效值集為 {0, 2, 3, ..., 36}。2 進位制整數的有效數字集為 {0, 1},3 進位制整數的有效數字集為 {0, 1, 2},依此類推。對於大於 10 的基數,有效數字包括字母字元,從 11 進位制整數的 Aa 開始,到 36 進位制整數的 Zz。字元的大小寫被忽略。

當前安裝的 C 區域設定可能會接受其他數字格式。

如果 base 的值為 0,則自動檢測數字基數:如果字首是 0,則基數是八進位制;如果字首是 0x0X,則基數是十六進位制;否則基數是十進位制。

如果減號是輸入序列的一部分,則從數字序列計算出的數值將被取反,就像結果型別中的一元減法一樣。

函式將 str_end 指向的指標設定為指向最後一個被解釋數字字元之後的字元。如果 str_end 是空指標,則忽略它。

如果 str 為空或不具有預期形式,則不執行轉換,並且(如果 str_end 不是空指標)str 的值儲存在 str_end 指向的物件中。

目錄

[編輯] 引數

str - 指向要解釋的空終止位元組字串的指標
str_end - 指向字元指標的指標
base - 被解釋整數值的*基數*

[編輯] 返回值

  • 如果成功,返回與 str 內容對應的整數值。
  • 如果轉換後的值超出相應返回型別的範圍,則發生範圍錯誤(將 errno 設定為 ERANGE)並返回 LONG_MAXLONG_MINLLONG_MAXLLONG_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.22.1.4 strtol, strtoll, strtoul 和 strtoull 函式 (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.22.1.4 strtol, strtoll, strtoul 和 strtoull 函式 (p: 251-252)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.22.1.4 strtol, strtoll, strtoul 和 strtoull 函式 (p: 344-345)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.20.1.4 strtol, strtoll, strtoul 和 strtoull 函式 (p: 310-311)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 4.10.1.5 strtol 函式

[編輯] 另請參閱

將位元組字串轉換為整數值
(函式) [編輯]
將位元組字串轉換為無符號整數值
(函式) [編輯]
(C95)(C99)
將寬字串轉換為整數值
(函式) [編輯]
將寬字串轉換為無符號整數值
(函式) [編輯]
C++ 文件 用於 strtol, strtoll