#include<stdio.h>
f(int a)
{ auto int b=0 ;
static int c=3 ;
b=b+1;
c=c+1;
return (a+b+c);
}
main( )
{ int a=2,i;
for(i=0;i<3;i++)printf("%d ",f(a));
}
//7 8 9 Press any key to continue
#include<stdio.h>
fun( )
{ int a=2;
static int b=4;
a=a+2;
b+=2;
printf("a=%d b=%d\n",a,b);
}
main( )
{ fun( );
fun( );
fun( );
}
/*
a=4 b=6
a=4 b=8
a=4 b=10
Press any key to continue
*/
本文通过两个C语言示例程序,深入解析了静态变量和局部变量在函数调用过程中的作用域、生命周期及内存分配差异。展示了静态变量如何保持其上一次函数调用后的值,而局部变量在每次函数调用时都会重新初始化。
660

被折叠的 条评论
为什么被折叠?



