中文名 :atoi
参 数 :字符串
返回值 : int
适用语言 : C/C++
头文件 : stdlib.h
原型:int atoi(const char *nptr);
atoi( ) 函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( )函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时(‘\0’)才结束转换,并将结果返回。如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回 0。
例:
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;
}
输出: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
本文详细介绍了C/C++中atoi函数的功能及使用方法。atoi用于将字符串转换为整数,通过示例代码展示了如何处理包含数字的字符串,包括带有正负号的情况。
1190

被折叠的 条评论
为什么被折叠?



