itoa和atoi的用法及功能

/*
itoa()函数有3个参数:第一个是要转换的数字,第二个参数是目标字符串,第三个参数是转移数字时所用的基数。

#include<stdlib.h>
#include<stdio.h>
int main()
{  
    int num = 10;  
    char str[100];  
    itoa(num, str, 2);     //将10转换成2进制 
    printf("%s\n", str);  
    system("pause");
    return 0; 
}


#include<stdlib.h>
#include<stdio.h>
int main()
{  
    int num = 9;  
    char str[100];  
    itoa(num, str, 8);    //将9转换成8进制 
    printf("%s\n", str);    //itoa输出的是一个一个字符。想变成一个数,double型,int型?看下面 
    system("pause");
    return 0; 
}
*/
//atoi把字符串转换成整型数 
#include<stdlib.h>
#include<stdio.h>
int main()
{  
    int num = 10;  
    char str[100];  
    int n = atoi(itoa(num, str, 2));   //此处即时其用法   
    printf("%d\n",n);  
    system("pause");
    return 0; 
}

/* 
如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时
停止转换,返回整型数。否则,返回零.
头文件: #include <stdlib.h>
*/
//再附上两个
#include<stdlib.h> 
#include<stdio.h> 
intmain(void) 
{ 
   float n; 
   char const *str="12345.67"; 
   n=atoi(str); 
   printf("string=%sinteger=%f\n",str,n); 
   return 0; 
}
执行结果:
string = 12345.67 integer = 12345.000000

#include<stdlib.h> 
#include<stdio.h> 
int main() 
{ 
    chara[]="-100"; 
    charb[]="123"; 
    intc; 
    c=atoi(a)+atoi(b); 
    printf("c=%d\n",c); 
    return 0; 
} 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值