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

来源:互联网 发布:怎样开淘宝网店需要多少钱 编辑:程序博客网 时间:2024/04/30 15:03
/*    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:

0 0