
atoi
atoi (表示 ascii to integer)是把字元串转换成整型数的一个函式,套用在电脑程式和办公软体中。int atoi(const char *nptr) 函式会扫描参数 nptr字元串,会跳过前面的空白字元(例如空格,tab缩进)等。如果 nptr不能转换成 int 或者 nptr为空字元串,那幺将返回 0。特别注意,该函式要求被转换的字元串是按十进制数理解的。atoi输入的字元串对应数字存在大小限制(与int类型大小有关),若其过大可能报错-1。
基本介绍
- 中文名:atoi
- 参数:字元串
- 返回值:int
- 适用语言:C/C++
- 头档案:#include <stdlib.h>
简介
C语言库函式名
atoi
原型
int atoi(const char *nptr);
UNICODE
_wtoi()
例子
(1)
//vs2013里调用printf函式请使用预处理命令#define _CRT_SECURE_NO_WARNINGS#include <stdlib.h>#include <stdio.h>int main(void){ int n; char *str = "12345.67"; n = atoi(str); printf("n=%d\n",n); return 0;}
输出:
n = 12345
(2)
//vs2013里调用printf函式请使用预处理命令#define _CRT_SECURE_NO_WARNINGS#include <stdlib.h>#include <stdio.h>int main(){ char a[] = "-100"; char b[] = "123"; int c; c = atoi(a) + atoi(b); printf("c=%d\n", c); return 0;}
执行结果:
c = 23