源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C语言 数与串之间转换的方法

  • 时间:2022-02-23 06:32 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C语言 数与串之间转换的方法
整数转换为字符串:char *itoa( int value, char *string,int radix); 小数转换为字符串:sprintf(串, 格式控制符列, 数据); 字符串转小数:double atof(const char *nptr); 字符串转整数:int atoi(const char *nptr); 测试代码:
[u]复制代码[/u] 代码如下:
#include<stdio.h>  #include<stdlib.h>  int main()  {      int a=2013420;      float b=2.054f;      double c=5.24;      char sa[20],sb[20],sc[20];      //将整数a转换为字符串      itoa(a,sa,10);      puts(sa);      //将浮点型数据转换为字符串      sprintf(sb,"%g",b);      puts(sb);      //将double型数据转换为字符串      sprintf(sc,"%lg",c);      puts(sc);      printf("========以下是串转换为数值=========\n");      char *s1="123",*s2="1.23";      printf("%d\n",atoi(s1));      printf("%g\n",atof(s2));      getchar();      return 0;  }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部