所有未加static前缀的全局变量和函数都具有全局可见性。为理解这句话,我举例来说明
static的第三个作用是默认初始化为0。其实全局变量也具备这一属性,因为全局变量也存储在静态数据区。在静态数据区,内存中所有的字节默认值都是0x00,某些时候这一特点可以减少程序员的工作量
#include
int g = 10;
main(){
int i =0;
void f1();
f1();
printf(" after first call n");
f1();
printf("after second call n");
f1();
printf("after third call n");}
void f1()
{
static int k=0;
int j = 10;
printf("value of k %d j %d",k,j);
k=k+10;
}
利用static实现全局变量赋值方法
存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化。共有两种变量存储在静态存储区:全局变量和static变量,只不过和全局变量比起来,static可以控制变量的可见范围,说到底static还是用来隐藏的
#include
void test1(void){
int count = 0;
printf("ntest1 count = %d ", ++count );
}void test2(void){
static int count = 0;
printf("ntest2 count = %d ", ++count );
}int main(void)
{
int i;
for(i = 0; i < 5; i++ )
{
test1();
test2();
}
return 0;
}
输出值为
test1 count = 1
test2 count = 1
test1 count = 1
test2 count = 2
test1 count = 1
test2 count = 3
test1 count = 1
test2 count = 4
test1 count = 1
test2 count = 5
洋果子店rose最新版本
洋果子店最新版是一款十分容易让人垂涎的开店经营类手游。游戏玩
明日大亨最新版
明日大亨手游,一个非常经典有趣的模拟经营类型的商战手游,在游
早安我的少年日服最新版2024
早安我的少年日服是一款治愈恋爱手游,清新明亮的游戏画面,让你
我的世界基岩版1.16正式版
我的世界基岩版是一款模拟经营手游,游戏采用的是经典像素玩法,
早安我的少年最新版2024
早安我的少年是一款养成恋爱手游,在这款游戏中,玩家们将可以体