/* itoa函数,有点烂自己都懒得看 */ #include <stdio.h> int myitoa(int n, char *str) { int base = 1; int i = 0; int len; int size; if(n == 0)//数位0时 { str[0] = '0'; str[1] = '\0'; return 0; } if(n < 0) { n = n * -1; base = -1; } while(n > 10) { str[i] = (n % 10) + '0'; n = n / 10; i++; } str[i] = n + '0'; size = i + 1; char temp[size];//构造一个暂存数组,把数据倒序转出 printf("位数:%d\n", size); for(len = 0; len < size; len++)//将数据倒序存储在暂存数组里 { temp[len] = str[i]; i--; } if(base == 1) { for(len = 0; len < size; len++) str[len] = temp[len]; } if(base == -1) { str[0] = '-'; i = 1; for(len = 0; len < size; len++) { str[i] = temp[len]; ++i; } } return 0; } int main() { int n = -12345; char ch[7]; myitoa(n, ch); ch[6] = '\0'; printf("%s\n", ch); }