#include"head_01.h"
// 01 static 的隐藏性
char a = 'A'; // 全局变量 但如果加了static 只能在本文件使用
void msg()
{
printf("Hello\n");
}
void static_func_01()
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
msg();
}
// 02 static 的隐藏性和持久性
int fun(void) {
static int count = 10; // 事实上此赋值语句从来没有执行过,如果没有static 每次都会执行赋值,有了static 它只为执行一次赋值
//int count = 10;
return count--;
}
int count = 1;
void static_func_02()
{
printf("global\t\tlocal static\n");
for (; count <= 10; ++count)
printf("%d\t\t%d\n", count, fun());
}
// 03 static 默认值为0
static int z;
int static_func_03()
{
static char str[10];
printf("integer: %d; string: (begin)%s(end)", z, str);// integer: 0; string: (begin)(end)
return 0;
}
void main()
{
static_func_01();
static_func_02();
static_func_03();
system("pause");
}
05 C语言 static
最新推荐文章于 2025-08-09 09:27:34 发布