- 论坛徽章:
- 0
|
本帖最后由 lhl161123 于 2013-06-06 18:53 编辑
题目:编写一个递归版本的itoa函数,即通过递归调用把整数转换为字符串。
#include <stdio.h>
#include <math.h>
void itoa(int n, char s[])
{
static int i;
if (n / 10)
itoa(n / 10, s);
else {
i = 0;
if (n < 0)
s[i++] = '-';
}
s[i++] = abs(n) % 10 + '0';
s = '\0';
}
main()
{
char s[10];
itoa(189271, s);
printf("%s\n", s);
itoa(-9012398, s);
printf("%s\n", s);
}
首先 网上找的答案 程序是可以得到想要的结果 只是不明白
请问高手void itoa(int n, char s[])函数的流程,我不理解为什么运行到函数最后's='\0'; }'不退出函数,反而跳到 ‘itoa(n / 10, s); ’ 然后继续执行;还有n的值为什么经过‘if (n / 10) itoa(n / 10, s); ' 递归后不是只剩下传进来前的高位的数字吗?怎么还可以还原? |
|