C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转 换为字符串的一个例子:
# include <stdio.h>
# include <stdlib.h>
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number ’num’ is %d and the string ’str’ is %s. \n" ,
num, str);
}
itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制...
itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:
char str[255];
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。
下列函数可以将整数转换为字符串:
----------------------------------------------------------
函数名 作 用
----------------------------------------------------------
itoa() 将整型值转换为字符串
itoa() 将长整型值转换为字符串
ultoa() 将无符号长整型值转换为字符串
一 atoi 把字符串转换成整型数
例程序:
#include <ctype.h>
#include <stdio.h>
int atoi (char s[]);
int main(void )
{
char s[100];
gets(s);
printf("integer=%d\n",atoi(s));
return 0;
}
int atoi (char s[])
{
int i,n,sign;
for(i=0;isspace(s[i]);i++)//跳过空白符
;
sign=(s[i]==’-’)?-1:1;
if(s[i]==’+’||s[i]==’ -’)//跳过符号
i++;
for(n=0;isdigit(s[i]);i++)
n=10*n+(s[i]-’0’);//将数字字符转换成整形数字
return sign *n;
}
二 itoa 把一整数转换为字符串
例程序:
#include <ctype.h>
#include <stdio.h>
void itoa (int n,char s[]);
//atoi 函数:将s转换为整形数
int main(void )
{
int n;
char s[100];
printf("Input n:\n");
scanf("%d",&n);
printf("the string : \n");
itoa (n,s);
return 0;
}
void itoa (int n,char s[])
{
int i,j,sign;
if((sign=n)<0)//记录符号
n=-n;//使n成为正数
i=0;
do{
s[i++]=n%10+’0’;//取下一个数字
}while ((n/=10)>0);//删除该数字
if(sign<0)
s[i++]=’-’;
s[i]=’\0’;
for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出
printf("%c",s[j]);
C语言itoa函数和atoi 函数的更多相关文章
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- [置顶]
C语言itoa()函数和atoi()函数详解(整数转字符C实现)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)【转载】
文章转载自https://www.cnblogs.com/bluestorm/p/3168719.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- memset和memcpy函数、atoi函数
memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...
- C语言itoa()函数和atoi()函数
以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio.h> # include <stdlib.h> void main (void) { ...
- itoa()函数和atoi()函数
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明.● itoa():将 ...
随机推荐
- 在eclipse下远程调试hadoop2.0
在<在eclipse下编译hadoop2.0源码>一文中,我详细介绍了如何在eclipse环境和命令行环境下编译hadoop2.0源代码,并简单介绍了如何构建hadoop环境,这篇文章将着 ...
- codeforces 546B
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- ios 打电话结束返回到应用中
在我们做打电话这个功能时,我们常常是调用这个方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel ...
- poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2743 Accepted: 955 De ...
- linux查看cpu温度
分类: linux系统 一.安装 sudo apt-get install lm-sensors 二.查看 linux@cdyemail:~$ sensors k10temp-pci-00c ...
- 查看Linux下网卡状态或 是否连接(转)
1) 通过mii-tool指令 [root@localhost root]# mii-tool eth0: negotiated 100baseTx-FD, link o ...
- 3 Ways to Preload Images with CSS, JavaScript, or Ajax---reference
Preloading images is a great way to improve the user experience. When images are preloaded in the br ...
- Analyzing UI Performance with Systrace 使用systrace工具分析ui性能
While developing your application, you should check that user interactions are buttery smooth, runni ...
- H.264视频在android手机端的解码与播放(转)
随着无线网络和智能手机的发展,智能手机与人们日常生活联系越来越紧密,娱乐.商务应用.金融应用.交通出行各种功能的软件大批涌现,使得人们的生活丰富多彩.快捷便利,也让它成为人们生活中不可取代的一部分.其 ...
- 【开源java游戏框架libgdx专题】-03-项目开发与调试
创建libgdx项目 下载项目配置工具 gdx-setup.jar 生成项目 导入Eclipse File -> Import -> Gradle -> Gradle Project ...