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

立即学习:https://edu.csdn.net/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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值