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

/*
    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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值