程序目的:实现c++整型和字符数组,字符串的相互转化

环境:dev-c++

源代码:

#include <iostream>
#include <cstdlib>
using namespace std;
main()
{
     int i1=123,i2,i3;
     float f1;
     char c1[20]="234.45",c2[20];
     string s1="456",s2;
      i2=atoi(c1);      //将字符数组转化为整数
     f1=atof(c1);      //将字符数组转化为浮点型
     itoa(i1,c2,10);   //将整型转化为字符数组  10为10进制的意思
     cout<<"字符数组转化为整型: "<<i2<<endl;
     cout<<"字符数组转化为浮点型: "<<f1<<endl;
     cout<<"整型转化为字符数组: "<<c2<<endl;
     strncpy(c2,s1.c_str(),s1.length());//字符串转化为字符数组
     i3=atoi(c2);
     cout<<"字符串转化为整型: "<<i3<<endl;
     system("pause");
}
截图:

摘自http://hi.baidu.com/isokill/blog/item/7e0620d6146f11c9a8ec9a5a.html

原文地址:https://www.cnblogs.com/xd502djj/p/2387742.html