atoi, atol, atoll
来自 cppreference.cn
在头文件 <stdlib.h> 中定义 |
||
int atoi ( const char* str ); |
(1) | |
long atol ( const char* str ); |
(2) | |
long long atoll( const char* str ); |
(3) | (C99 起) |
解释由 str 指向的字节字符串中的整数值。隐含的基数总是 10。
丢弃所有空白字符,直到找到第一个非空白字符,然后尽可能多地获取字符以形成有效的整数数字表示,并将其转换为整数值。有效的整数值包含以下部分
- (可选) 加号或减号
- 数字
如果结果的值不能表示,即转换后的值超出相应返回类型的范围,则行为未定义。
目录 |
[编辑] 参数
str | - | 指向要解释的空终止字节字符串的指针 |
[编辑] 返回值
成功时,返回对应于 str 内容的整数值。
如果无法执行转换,则返回 0。
[编辑] 注意
名称代表“ASCII to integer”(ASCII 到整数)。
[编辑] 示例
运行此代码
#include <stdio.h> #include <stdlib.h> int main(void) { printf("%i\n", atoi(" -123junk")); printf("%i\n", atoi(" +321dust")); printf("%i\n", atoi("0")); printf("%i\n", atoi("0042")); // treated as a decimal number with leading zeros printf("%i\n", atoi("0x2A")); // only leading zero is converted discarding "x2A" printf("%i\n", atoi("junk")); // no conversion can be performed printf("%i\n", atoi("2147483648")); // UB: out of range of int }
可能的输出
-123 321 0 42 0 0 -2147483648
[编辑] 参考
- C23 标准 (ISO/IEC 9899:2024)
- 7.22.1.2 The atoi, atol, and atoll functions (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.22.1.2 The atoi, atol, and atoll functions (p: 249)
- C11 标准 (ISO/IEC 9899:2011)
- 7.22.1.2 The atoi, atol, and atoll functions (p: 341)
- C99 标准 (ISO/IEC 9899:1999)
- 7.20.1.2 The atoi, atol, and atoll functions (p: 307)
- C89/C90 标准 (ISO/IEC 9899:1990)
- 4.10.1.2 The atoi function
- 4.10.1.3 The atol function
[编辑] 另请参阅
(C99) |
将字节字符串转换为整数值 (函数) |
(C99) |
将字节字符串转换为无符号整数值 (函数) |
(C95)(C99) |
将宽字符串转换为整数值 (函数) |
(C95)(C99) |
将宽字符串转换为无符号整数值 (函数) |
C++ documentation for atoi, atol, atoll
|