strtol, strtoll
从 zh.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 所指向的字节字符串中的整数值。
丢弃所有空白字符(通过调用 isspace 识别),直到找到第一个非空白字符,然后取尽可能多的字符构成一个有效的 n 进制(其中 n=base)整数表示形式,并将其转换为整数值。有效的整数值包含以下部分:
- (可选)正负号
- (可选)前缀(
0)表示八进制(仅在基数为8或0时适用) - (可选)前缀(
0x或0X)表示十六进制(仅在基数为16或0时适用) - 一个数字序列
基数有效值的范围是 {0, 2, 3, ..., 36}。基数 2 整数的有效数字集是 {0, 1},基数 3 整数的有效数字集是 {0, 1, 2},依此类推。对于大于 10 的基数,有效数字包括字母字符,从基数 Aa 整数的 11 开始,一直到基数 Zz 整数的 36。字符的大小写被忽略。
当前安装的 C 本地环境可能接受额外的数值格式。
如果 base 的值为 0,则数字基数自动检测:如果前缀是 0,则基数为八进制;如果前缀是 0x 或 0X,则基数为十六进制;否则基数为十进制。
如果输入序列包含负号,则根据数字序列计算出的数值将按照结果类型进行一元负号取反。
这些函数将 str_end 所指向的指针设置为指向最后一个被解释的数字字符之后的位置。如果 str_end 是空指针,则忽略它。
如果 str 为空或不具有预期形式,则不进行转换,并且(如果 str_end 不是空指针)将 str 的值存储到 str_end 所指向的对象中。
参数
| str | - | 指向要解释的空终止字节字符串的指针 |
| str_end | - | 指向字符指针的指针 |
| base | - | 被解释的整数值的基数 |
返回值
- 如果成功,返回与
str内容对应的整数值。 - 如果转换后的值超出返回类型的范围,则发生范围错误(将 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 strtol、strtoll、strtoul 和 strtoull 函数 (p: 362-363)
- 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 函数
参阅
(C99) |
将字节字符串转换为整数值 (函数) |
(C99) |
将字节字符串转换为无符号整数值 (函数) |
(C95)(C99) |
将宽字符串转换为整数值 (函数) |
(C95)(C99) |
将宽字符串转换为无符号整数值 (函数) |
C++ 文档 关于 strtol、strtoll
| |