东瑜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

递归例子如下:

 1 #include <stdio.h>
 2 /*函数声明*/
 3 void digui(int n);
 4 
 5 int main()
 6 {
 7   int n=10;
 8   digui(n);
 9   return 0;
10 }
11 
12 void digui(int n)
13 {
14   printf("level1-value of %d\n",n);
15   if(n>2){
16     digui(n-1);
17   } 
18   printf("level2-value of %d\n",n);
19 }

 

程序结果如下:

[zsd@TOMCAT ~]$ ./test03
level1-value of 10
level1-value of 9
level1-value of 8
level1-value of 7
level1-value of 6
level1-value of 5
level1-value of 4
level1-value of 3
level1-value of 2
---------------------------邪恶的分割线------------------------
level2-value of 2
level2-value of 3
level2-value of 4
level2-value of 5
level2-value of 6
level2-value of 7
level2-value of 8
level2-value of 9
level2-value of 10

通过gdb的调试,对代码的16行和18行设置断点,gdb执行的效果如下:

(gdb) run
Starting program: /home/zsd/test03debug 
level1-value of 10

Breakpoint 1, digui (n=10) at test03.c:16
16          digui(n-1);                  //开始第一次向下递归,递归数为9
(gdb) 
(gdb) continue
Continuing.
level1-value of 9

Breakpoint 1, digui (n=9) at test03.c:16
16          digui(n-1);                 //向下递归,递归数为8
(gdb) continue
Continuing.
level1-value of 8

Breakpoint 1, digui (n=8) at test03.c:16
16          digui(n-1);
(gdb) continue
Continuing.
level1-value of 7

Breakpoint 1, digui (n=7) at test03.c:16
16          digui(n-1);
(gdb) continue
Continuing.
level1-value of 6

Breakpoint 1, digui (n=6) at test03.c:16
16          digui(n-1);
(gdb) continue
Continuing.
level1-value of 5

Breakpoint 1, digui (n=5) at test03.c:16
16          digui(n-1);
(gdb) continue
Continuing.
level1-value of 4

Breakpoint 1, digui (n=4) at test03.c:16
16          digui(n-1);
(gdb) continue
Continuing.
level1-value of 3

Breakpoint 1, digui (n=3) at test03.c:16
16          digui(n-1);                 //一直到这里,递归数为2.这个时候,上面递归的每一个函数digui(2),digui(3)....digui(10)有最后一条printf("level2-value of %d\n",n);语句没有执行。
(gdb) continue
Continuing.
level1-value of 2

Breakpoint 2, digui (n=2) at test03.c:18   //digui(2)执行printf("level2-value of %d\n",n);语句
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 2

Breakpoint 2, digui (n=3) at test03.c:18   //digui(3)执行printf("level2-value of %d\n",n);语句
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 3

Breakpoint 2, digui (n=4) at test03.c:18   //以上述递推,一直到digui(10)执行完毕。
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 4

Breakpoint 2, digui (n=5) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 5

Breakpoint 2, digui (n=6) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 6

Breakpoint 2, digui (n=7) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 7

Breakpoint 2, digui (n=8) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 8

Breakpoint 2, digui (n=9) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 9

Breakpoint 2, digui (n=10) at test03.c:18
18        printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of 10

Program exited normally.

 

posted on 2018-10-26 16:14  东瑜  阅读(322)  评论(0编辑  收藏  举报
\\页脚html代码