QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2363|回复: 2

我靠,linux 下的 itoa() 函数还得自己写

[复制链接]
发表于 2011-1-11 19:49:31 | 显示全部楼层 |阅读模式
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. void      itoa (int n,char s[]);
  4. //atoi 函数:将s转换为整形数
  5. int main(void )
  6. {   
  7. int n;
  8. char s[100];

  9. printf("Input n:\n");
  10. scanf("%d",&n);

  11.         printf("the string : \n");
  12.         itoa (n,s);
  13. return 0;
  14. }
  15. void itoa (int n,char s[])
  16. {
  17. int i,j,sign;

  18. if((sign=n)<0)//记录符号
  19.       n=-n;//使n成为正数
  20.         i=0;
  21. do{
  22.       s[i++]=n%10+'0';//取下一个数字
  23. }while ((n/=10)>0);//删除该数字

  24. if(sign<0)
  25.       s[i++]='-';
  26. s[i]='\0';
  27. for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出
  28.       printf("%c",s[j]);

  29. }
复制代码
发表于 2011-1-12 09:50:54 | 显示全部楼层
这玩意本来就不是ansi c标准的。
回复

使用道具 举报

CecilHarvey 该用户已被删除
发表于 2011-1-12 11:32:13 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-25 03:15 , Processed in 0.074488 second(s), 24 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表