命名空间
变体
操作

strtol, strtoll

来自 cppreference.cn
< c‎ | string‎ | byte
定义于头文件 <stdlib.h>
long      strtol( const char*          str, char**          str_end, int base );
(until C99)
long      strtol( const char* restrict str, char** restrict str_end, int base );
(since C99)
long long strtoll( const char* restrict str, char** restrict str_end, int base );
(since 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 locale 可能会接受其他数值格式。

如果 base 的值为 0,则自动检测数值基数:如果前缀是 0,则基数为八进制;如果前缀是 0x0X,则基数为十六进制;否则基数为十进制。

如果负号是输入序列的一部分,则从数字序列计算出的数值将被取反,如同在结果类型中使用 一元负号

这些函数将 str_end 指向的指针设置为指向已解释的最后一个数字字符之后的字符。如果 str_end 是空指针,则会被忽略。

如果 str 为空或不具有预期的格式,则不执行转换,并且(如果 str_end 不是空指针)str 的值将存储在 str_end 指向的对象中。

目录

[编辑] 参数

str - 指向要解释的空终止字节字符串的指针
str_end - 指向字符指针的指针
base - 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 The strtol, strtoll, strtoul, and strtoull functions (页码:待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (页码:251-252)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (页码:344-345)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions (页码:310-311)
  • C89/C90 标准 (ISO/IEC 9899:1990)
  • 4.10.1.5 The strtol function

[编辑] 参见

将字节字符串转换为整数值
(函数) [编辑]
将字节字符串转换为无符号整数值
(函数) [编辑]
(C95)(C99)
将宽字符串转换为整数值
(函数) [编辑]
将宽字符串转换为无符号整数值
(函数) [编辑]
C++ 文档 for strtol, strtoll