这道题为什么输出结果 ( 6 15 15 )

来源:百度知道 编辑:UC知道 时间:2024/07/08 03:41:51
#include<stdio.h>

int d=1;
fun(int p){
static int d=5;
d+=p;
printf("%d",d);
return (d);
}
main()
{
int a=3;
printf("%d\n",fun(a+fun(d)));
}

首先static变量只有一次初始化,之后的值会保留的,知道这一点就好理解了

printf("%d\n",fun(a+fun(d)));,这个语句首先调用fun(d),d=1,所以在fun函数中p=1,static d=5,则新的static d=6,首先输出6,并且函数返回6

然后调用fun(a+fun(d))等价于fun(a+6)即fun(9)
那么fun函数中的static d现在是6,则新的static d=6+9=15,所以首先输出15,然后函数返回15,printf("%d\n",15); 输出15

最后结果就是6 15 15

为什么输出结果?还是。。