字符串和数字之间的转换

#include<stdlib.h>

字符串转数字

int atoi(const char *nptr);

int atol(const char *nptr);==long int strtol(const char *nptr, char **endptr,int base);

long int strtol(const char *nptr, char **endptr,int base);base表示进制,endptr返回不符合条件而终止的字符的指针,出现非数字或字符串结束符终止,出现数字或正符号才开始转换。

double atof(const char *nptr);

double strtod(const char *nptr,char **endptr);

数字转字符串

char *gcvt(double number,size_t ndigits,char *buf);number转成字符串,放入buf所指向的缓冲区,ndigits为能显示的位数。

 

#include<stdio.h>
#include<stdlib.h>
int main(){
//int atoi,long  atol=long int strtol
char a[]="-100";
char b[]="0x20";
int c;
c=atoi(a)+atoi(b);
printf("c=%d\n",c);
//long int strtol(const char *nptr,const char **endptr,int base)
char two[]="10000";
char twelve[]="20000";
char sixteen[]="FFFF";
printf("two=%ld\n",strtol(two,NULL,2));
printf("twelve=%ld\n",strtol(twelve,NULL,2));
printf("sixteen=%ld\n",strtol(sixteen,NULL,2));
//double atof
char doub[]="188.888";
printf("double=%f\n",atof(doub));
//strtod
printf("strtod double=%f\n",strtod(doub,NULL));
//char *gcvt
double gcvtd=666.666;
char str[64];
gcvt(gcvtd,6,str);
printf("gcvt str:%s",str);
return 0;   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值