Fun9函数中,j是static int,它的值一直增加。在Fun9(a,s)后再去计算Fun(a,C),Fun9(a,s)中的j会对Fun9(a,s)中的j产生影响。
所以在Fun(a,C)的时候,j并不是从0开始的,所以相当于C中前面的部分没有赋值。所以会出现烫烫烫
代码一:(这个函数实现逆序输出数字,比如n=123,输出的s是"321")
void Funt9(int n,char s[])
{
if(n==0)
*s = 0;
else
{
*s = '0'+ n%10;
Funt9(n/10,s+1);
}
}
代码二:(正序输出,n=123,s输出"123")
#include <stdio.h>
void Funt9(int n,char s[])
{
int i,k;
static int j = 0;
char c;
if(n == 0)
{
s[j] = 0;
//这里实现字符串s的逆序,j是字符串s的长度
for(i=0,k=j-1;i<k;i++,k--)
{
c = s[i];
s[i] = s[k];
s[k] = c;
}
j = 0; //这里j置0,避免对下一次调用Funt9时产生影响。
}else
{
s[j] = '0'+ n%10;
j++;
Funt9(n/10,s);
}
}
int main()
{
char s[10],C[10];
int a = 12345;
int b =789;
int j = 0;
Funt9(a,s); //得到的s是正序输出
Funt9(b,C);
printf("%s\n",s);
printf("%s\n",C);
return 0;
}