atoi函数

 函数名: atoi
功 能: 把字符串转换成整型数
函数说明: atoi()会扫描参数nptr字符串,检测到第一个数字或正负符号时开始做类型转换,之后检测到非数字或结束符 \0 时停止转换,返回整型数。
用 法: int atoi(const char *nptr);
需要用到的头文件: #include <stdlib.h>
程序例:
1)
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
执行结果
string = 12345.67 integer = 12345
2)
#include <stdlib.h>
#include <stdio.h>
int mian()
{
char a[] = "-100" ;
char b[] = "123" ;
int c ;
c = atoi( a ) + atoi( b ) ;
printf("c = %d\n", c) ;
return 0;
}
执行结果
c = 23
原文地址:https://www.cnblogs.com/danghuijian/p/4400903.html