1、概念
函数递归调用:函数可以调用函数本身(不要用main()调用main(),不是不能这么做,而是不建议,往往得不到你想要的结果)。
递归的优点:递归给某些编程问题提供了最简单的方法。
递归的缺点:一个有缺陷的递归会很快耗尽计算机的资源,递归的程序难以理解和维护
2、demo
1 十进制转二进制
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void to_bin(int n)
{
int a = n % 2;
if (n == 0)
return;
to_bin(n/2);
printf("%d ",a);
}
int main()
{
to_bin(54);
system("pause");
return 0;
}
2 字符串反转
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void print_str(char *str,char *buf)
{
if (*str == 0)
return;
print_str(str+1,buf);
//printf("%c",*str);
strncat(buf,str,1);
}
int main()
{
char str[] = "123456";
char buf[128] = {0};
print_str(str,buf); // 654321
printf("%s\n", buf); // 123456
system("pause");
return 0;
}