东瑜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  135 随笔 :: 0 文章 :: 11 评论 :: 21万 阅读

作者:@张扶摇
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhangshengdong/p/9857034.html


目录

递归例子如下:

复制代码
 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   东瑜  阅读(328)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
\\页脚html代码
点击右上角即可分享
微信分享提示