static关键字创造了静态变量,静态变量是该变量在内存中的位置不变,而不是大小不变。
例如:
#include <stdio.h>
void trystat(void);
int main(void)
{
int count = 1;
for (count = 1; count <= 3; count++)
{
printf("Here comes iteration %d:\n", count);
trystat();
}
return 0;
}
void trystat(void)
{
int fade = 1;
static int stay = 1;
printf("fade = %d stay = %d\n", fade++, stay++);
}
可以看到在运行结果中 fade在每次打印值后都++ 但在下个循环中又变为1;
而stay的值在打印后++,++的结果都保留下来。