您的位置:首页 > 编程语言 > C语言/C++

c/c++中如何将字符串转换为int 型 long 型 和double型

2020-01-12 19:20 246 查看

将字符串转换为数字

int long double 型分别对应的函数为atoi atol atof

#include<iostream>
using namespace std;
int main()
{
char str1[10] = "12345";
char str2[20] = "123435334";
char str3[10] = "100.32";
char str4[10] = "30da4d";
int x1 = atoi(str1);
cout << x1<<endl;
long x2 = atol(str2);
cout << x2 << endl;;
double x3 = atof(str3);
cout << x3<<endl;
int x4 = atoi(str4);
cout << x4;
return 0;
}

执行结果


注意
当字符串中有除了数字以外的字符情况看x4的输出结果

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Destiny丶瓶邪 发布了13 篇原创文章 · 获赞 13 · 访问量 1925 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: