您的位置:首页 > 其它

精彩百例:函数的递归调用

2015-03-28 11:20 399 查看
/*
filename: the recursion
function: convert the number to char
*/
# include <stdio.h>

void convert_num(int num);
int main(void)
{
int num;
printf("Please input the number what you need convert: ");
scanf("%d", &num);
printf("\nThe number is %d.\n", num);
/*if the number is negative ,add minus front of the number*/
if(num < 0)
{
putchar('-');
num = -num;
}
/*convert the number to char*/
printf("convertting the number .....\n");
convert_num(num);

return 0;
}
/*convert the number to char*/
void convert_num(int num)
{
int i;
if((i=num/10) != 0)
convert_num(i);

putchar((num%10)+'0');
}
/*
递归:
当条件成立,进入第二层调用,当下一次条件成立,进入第三层调用
当条件不再成立,执行判断之后的语句,执行完之后,跳出最深层的函数(这里指第三层),
进入次一级的函数(这里指第二层),
一直这样循环,直到跳出所有的函数
*/

result:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 递归