关于static 的研究 与递归调用的输出

#include "stdio.h"
int fun(int ,int);
// 用户自定义函数在main函数之后使用,
//就必须在main函数之前进行声明
main()
{
int k=4,m=1,p;
p=fun(k,m);
printf("%-2d",p);
p=fun(k,m);
printf("%d",p);
}
int fun(int a,int b)
{
static int m,i=2; //static 作用之一就是保存上次执行的结果
i+=m+1;
m=i+a+b;
return m;}


static的作用: 1.保存上次执行的结果 2.static int m; 这里默认m的初始值为0,即默认

值是0

递归调用以后递归输出:
#include "stdio.h"
void fun(char *a)
{ if(*a)
{    fun(1+a); // 直到元素的下标为4,则a[4], a+1
//则a[5]为空则输出a[4],a[2],a[1],递归调用递归输出
printf("%c",*a);
}
}
void main()
{
char s[10]="12345";
printf("处理前的字符串 :%s\n",s);
printf("处理后的字符串 :");
fun(s);
printf('\n');

 

posted @ 2016-05-03 19:57  #ifndef  阅读(340)  评论(0编辑  收藏  举报