C语言static用法之函数内部static变量

Static local variables

When a variable in a function is static, the variable preserves its value between function calls.

#include 
#include 

void fun2() {
	static int count = 0;
	count++;
	printf("%d\n", count);

}

void fun() {
	int count = 0;
	count++;
	printf("%d\n", count);

}

int main() {
	fun();
	fun();
	fun();

	fun2();
	fun2();
	fun2();
	return EXIT_SUCCESS;
}


参考: https://linuxhint.com/static_c/

你可能感兴趣的:(linux,c语言,c++,开发语言)