C语言之递归
递归例子如下:
#include <stdio.h>
/*函数声明*/
void digui(int n); int main()
{
int n=;
digui(n);
return ;
} void digui(int n)
{
printf("level1-value of %d\n",n);
if(n>){
digui(n-);
}
printf("level2-value of %d\n",n);
}
程序结果如下:
[zsd@TOMCAT ~]$ ./test03
level1-value of
level1-value of
level1-value of
level1-value of
level1-value of
level1-value of
level1-value of
level1-value of
level1-value of
---------------------------邪恶的分割线------------------------
level2-value of
level2-value of
level2-value of
level2-value of
level2-value of
level2-value of
level2-value of
level2-value of
level2-value of
通过gdb的调试,对代码的16行和18行设置断点,gdb执行的效果如下:
(gdb) run
Starting program: /home/zsd/test03debug
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-); //开始第一次向下递归,递归数为9
(gdb)
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-); //向下递归,递归数为8
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-); //一直到这里,递归数为2.这个时候,上面递归的每一个函数digui(2),digui(3)....digui(10)有最后一条printf("level2-value of %d\n",n);语句没有执行。
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:18 //digui(2)执行printf("level2-value of %d\n",n);语句
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:18 //digui(3)执行printf("level2-value of %d\n",n);语句
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:18 //以上述递推,一直到digui(10)执行完毕。
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Program exited normally.
C语言之递归的更多相关文章
- 使用Python语言理解递归
递归 一个函数在执行过程中一次或多次调用其本身便是递归,就像是俄罗斯套娃一样,一个娃娃里包含另一个娃娃. 递归其实是程序设计语言学习过程中很快就会接触到的东西,但有关递归的理解可能还会有一些遗漏,下面 ...
- C语言中递归什么时候能够省略return引发的思考:通过内联汇编解读C语言函数return的本质
事情的经过是这种,博主在用C写一个简单的业务时使用递归,因为粗心而忘了写return.结果发现返回的结果依旧是正确的.经过半小时的反汇编调试.证明了我的猜想,如今在博客里分享.也是对C语言编译原理的一 ...
- TINY语言采用递归下降分析法编写语法分析程序
目录 自顶向下分析方法 TINY文法 消左提左.构造first follow 基本思想 python构造源码 运行结果 参考来源:聊聊编译原理(二) - 语法分析 自顶向下分析方法 自顶向下分析方法: ...
- C#语言基础——递归
递归 一.概念conception: 函数体内调用本函数自身,直到符合某一条件不再继续调用. 二.应满足条件factor: (1)有反复执行的过程(调用自身): (2)有跳出反复执行过程的条件(函数出 ...
- c语言例子递归与整数逆序
例一 #include <stdio.h> //将一整数逆序后放入一数组中(要求递归实现) void convert(int *result, int n) { if(n>=10) ...
- C语言数据结构----递归的应用(斐波拉契数列、汉诺塔、strlen的递归算法)
本节主要说了递归的设计和算法实现,以及递归的基本例程斐波拉契数列.strlen的递归解法.汉诺塔和全排列递归算法. 一.递归的设计和实现 1.递归从实质上是一种数学的解决问题的思维,是一种分而治之的思 ...
- C语言数据结构----递归的应用(八皇后问题的具体流程)
本节主要讲八皇后问题的基本规则和递归回溯算法的实现以及具体的代码实现和代码分析. 转载请注明出处.http://write.blog.csdn.net/postedit/10813257 一.八皇后问 ...
- c语言,递归翻转一个单链表,c实现单链表
目的:主要是练习c里面单链表的实现,递归思想复习; #include <stdlib.h> #include <stdio.h> typedef struct _Node{// ...
- 二叉排序树插入C语言版 递归步骤理解
//二叉排序树 插入 (纯C语言实现) BTNode * BSTInsert2(BTNode *bt,int key){ //为什么纯C语言实现中 if(bt==NULL){ //要写成 bt-> ...
随机推荐
- mysql数据库内容相关操作
第一:介绍 mysql数据内容的操作主要是: INSERT实现数据的插入 UPDATE实现数据的更新 DLETE实现数据的删除 SELECT实现数据的查询. 第二:增(insert) 1.插入完整的数 ...
- ggplot2 aes函数map到data笔记
.all_aesthetics <- c("adj", "alpha", "angle", "bg", " ...
- iptables安装
1.安装iptable iptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y ipt ...
- pyqt5之简单窗口的创建
在学完tkinter后,发现tkinter在布局方面特别的不方便(Tkinter资料:http://effbot.org/tkinterbook/tkinter-index.htm),因此学习pyqt ...
- Tomcat简单优化
解决 有两种解决办法: 1)在Tomcat环境中解决 可以通过配置JRE使用非阻塞的Entropy Source. 在catalina.sh中加入这么一行:-Djava.security.egd=fi ...
- Signed Distance Field Shadow in Unity
0x00 前言 最近读到了一个今年GDC上很棒的分享,是Sebastian Aaltonen带来的利用Ray-tracing实现一些有趣的效果的分享. 其中有一段他介绍到了对Signed Distan ...
- [IOT] 自制蓝牙工牌办公室定位系统 (二)—— 基于ESP32的蓝牙信号扫描系统
前面章节: 自制蓝牙工牌办公室定位系统 (一)-- 阿里物联网平台概览及打通端到云(硬核·干货) 目录: 1.蓝牙广播简介 2.蓝牙扫描简介 3.基于蓝牙广播和蓝牙扫描常见应用 4.ESP32 ...
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...
- React Native调试实用技巧,React Native开发者必会的调试技巧
在做React Native开发时,少不了的需要对React Native程序进行调试.调试程序是每一位开发者的基本功,高效的调试不仅能提高开发效率,也能降低Bug率.本文将向大家分享React Na ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...