命名空间
变体
操作

strtol, strtoll

来自 cppreference.cn
< 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