C 语言递归
递归是什么?
递归就是函数定义内又调用自己
语法如下
void test(...)
{
...
test(...);
...
}
注意:
递归是一个容易发生死循环,因此在写代码的时候要特别注意
示例如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <setjmp.h>
void logNumber(int a)
{
printf("当前a = %d \n", a);
if(a < 10)
{
a++;
logNumber(a);
}
}
int main()
{
logNumber(0);
return 0;
}
运行结果为
当前a = 0
当前a = 1
当前a = 2
当前a = 3
当前a = 4
当前a = 5
当前a = 6
当前a = 7
当前a = 8
当前a = 9
当前a = 10