`
luckyclouds
  • 浏览: 109294 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

itoa

    博客分类:
  • c
 
阅读更多
#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]);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics