立即学习:https://edu.csdn/course/play/10534/394482?utm_source=blogtoedu

static 存储类
指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。
static 修饰符也可以应用于全局变量。当 static 修饰全局变量时,会使变量的作用域限制在声明它的文件内。全局声明的一个 static 变量或方法可以被任何函数或方法调用,只要这些方法出现在跟 static 变量或方法同一个文件中。
#include <stdio.h>

extern test();
static int g = 100;
int x = 200;

void test_static() {
	int a = 10;
	static int b = 10; // 只会初始化一次,在整个程序的生命周期内不会销毁, 全局靜態區

	a++;
	b++;

	printf("a = %d\n", a);
	printf("b = %d\n", b);
}

void test_global() {
	printf("g = %d\n", g);
	printf("x = %d\n", x);
}

void test_static_1() {
	printf("g = %d\n", g);
	printf("x = %d\n", x);
}

int main() {
	//test_static();
	//test_static();
	//test();
	test_global();
	test_static_1();

	return 0;
}

#include <stdio.h>

extern g;
extern x;

static void test() {
	printf("g = %d\n", g);
	printf("x = %d\n", x);
}

 

更多推荐

学习笔记(81):C语言入门到精通-存储类型修饰符-下