现在的位置: 首页 > 综合 > 正文

c 全局静态变量的实例解释其作用域

2019年01月12日 ⁄ 综合 ⁄ 共 453字 ⁄ 字号 评论关闭

glovar.h 文件

#pragma once
static int glo;

one.h 文件

void getone();

one.cpp 文件

#include "stdafx.h"
#include "glovar.h"
#include "one.h"

void getone()
{
	glo = 13;
	printf("one: glo is %d \n",glo);
}

two.h 文件

void gettwo();

two.cpp 文件

#include "stdafx.h"
#include "glovar.h"
#include "one.h"
#include "two.h"

void gettwo()
{
	glo = 15;
	getone();
	printf("two: glo is %d \n",glo);
}

main.cpp 文件

#include "stdafx.h"
#include "one.h"
#include "two.h"
int _tmain(int argc, _TCHAR* argv[])
{
	gettwo();
	return 0;
}

运行结果:

one: glo is 13
two:  glo is 15

抱歉!评论已关闭.