解决方案1:
加 ‘0’ 后强制类型转换
如下程序,输入数字,转换成
字符串输出
#include <stdio.h>
int main(void)
{
int n,i;
char ch[100];
while(scanf("%d",&n))
{
int top = 0;
while(n > 0)
{
ch[top++] = (char)(n%10 + '0');
n /= 10;
}
for(i = top-1; i >= 0; i--)
{
printf("%c",ch[i]);
}
puts("");
}
}
解决方案2:
字符a的ASCII码值为 97
字符0的ASCII吗值为 48
‘0‘+49=’a‘
解决方案3:
使用sprintf函数可以将数字按指定格式转换成字符串,与printf函数用法差不多.