atoi, atol, atoll_C语言中文网
| 定义于头文件 |
||
| int atoi( const char *str ); |
||
| long atol( const char *str ); |
||
| long long atoll( const char *str ); |
(C99 起) | |
转译 str 所指的字节字符串中的整数值。
舍弃任何空白符,直至找到首个非空白符,然后接收尽可能多的字符以组成合法的整数表示,并转换之为整数值。合法的整数值含下列部分:
- (可选) 正或负号
- 数位
参数
返回值
成功时为对应 str 内容的整数值。若转换的值落在对应的返回类型范围外,则返回值未定义。若无法进行转换,则返回 0 。
示例
#include <stdio.h> #include <stdlib.h> int main(void) { printf("%i\n", atoi(" -123junk")); printf("%i\n", atoi("0")); printf("%i\n", atoi("junk")); // 无可进行的转换 printf("%i\n", atoi("2147483648")); // UB :在 int 范围外 }
输出:
引用
- C11 standard (ISO/IEC 9899:2011):
- 7.22.1.2 The atoi, atol, and atoll functions (p: 341)
- C99 standard (ISO/IEC 9899:1999):
- 7.20.1.2 The atoi, atol, and atoll functions (p: 307)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.10.1.2 The atoi function
- 4.10.1.3 The atol function