标签:itoa
#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> char *my_itoa(int value, char *s) { int sign; int i = 0,j,k; char *tmp = (char *)malloc(sizeof(char)*strlen(s)); if((sign=value) < 0) //记录符号,使value为正数 value = -value; while(value) { tmp[i++] = value%10 +'0'; value = value/10; //取下一个数字 } if(sign < 0) tmp[i++] = '-'; for(j=i-1,k=0;j>=0;j--,k++) //生成的数字是逆序的,所以要逆序输出 { s[k] = tmp[j]; } s[k] = '\0'; return s; } int main(void) { int value; char str[100]; while(1) { printf("please input a number:"); scanf("%d",&value); printf("%s\n",my_itoa(value,str)); } }
标签:itoa
原文地址:http://blog.csdn.net/a1232345/article/details/46533077