int atoi(char *s)
如果字符串内容是整数就返回该整数,否则返回0
double atof(char *s)
同上,不过返回浮点型
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
char ch1[10] = "123456";
int a = atoi(ch1);
cout<<a<<endl;
char ch2[10] = "123.456";
a = atoi(ch2);
double b = atof(ch2);
cout<<a<<endl<<b<<endl;
char ch3[10] = "abc12";
a = atoi(ch3);
cout<<a;
return 0;
}
输出结果:
123456
123
123.456
0
本文通过一个简单的C语言程序示例介绍了如何使用atoi和atof函数将字符串转换为整数和浮点数。程序演示了对于不同格式的字符串输入,atoi和atof函数的处理方式,并展示了具体的输出结果。
2143

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



