環境: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