0
点赞
收藏
分享

微信扫一扫

static的用处

那小那小 2022-04-13 阅读 62
c语言
#include <stdio.h>
void say_hello();
int main(){
	int i;
	for(i=0;i<5;i++){
		say_hello();
	}
	return 0;
}

void say_hello(){
	static int num_calls = 1;
	printf("%d",num_calls);
	num_calls++;
}

输出结果为12345

如果删掉static,即

#include <stdio.h>
void say_hello();
int main(){
	int i;
	for(i=0;i<5;i++){
		say_hello();
	}
	return 0;
}

void say_hello(){
	int num_calls = 1;
	printf("%d",num_calls);
	num_calls++;
}

输出结果为11111

举报

相关推荐

0 条评论