打印整数数字
Print an integer using only putchar. Try doing it without using extra storage.
1)
void printInt(int a)
{
int b = a;
char *str;
int i = 1;
int len = 0;
while (b) {
b /= 10;
i *= 10;
len++;
}
i /= 10;
while (i > 0) {
putchar(a/i + 48);
a = a%i;
i /= 10;
}
}
2)
This can be done by recursion. Since the number of recursive calls is not significant, it does not affect the performance much.
void printnumber(int i)
{
if(i == 0)
return;
printnumber(i/10);
putchar('0' + i%10);
}