#include
#include
void to_str(n)
int n;
{ char s[10];
int i = 0;
if(n { putchar('-');
n = -n;
}
do
{ s[i++] = n%10 +'0';
n/=10;
}
while(n>0);
while(i--)putchar(s[i]);
}
main()
{ int x,y;
scanf("%d",&x);
to_str(x);
}
这里面的{ s[i++] = n%10 +'0';这行不懂,为什么要加'0',不加就不行呢,请赐教。
就是加上字符‘0’的其实位置,其他数字字符相对于他多了多少
道理很简单,利用ascii码的连续排列规律,将数字变成ascii码。
相对于'0','1'的ascii码是'0'+1,其余类推。
‘0’在ascii中的位置就是0x48,0x48~0x57是字符0到9对应的位置,不加‘0’,你只会打印出ascii表中0到9位置对应的字符,而不是0到9的字符