您的位置:首页 > 编程语言 > C语言/C++

C语言中关键字extern的一个作用(ZTE)

2012-09-26 20:56 190 查看
运行下面一段程序后,变量x的值是多少?

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
int x = 5;
int foo(void)
{
int x = 3;
{
extern int x;
}
return x;
}

int main(void)
{
cout<<"x="<<foo()<<endl;
return 0;
}


//运行结果:



#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
int x = 5;
int foo_1(void)
{
extern int x;
return x;
}

int main(void)
{
cout<<"x="<<foo_1()<<endl;
return 0;
}
//运行结果



总结:关键字extern声明的变量的作用域扩大了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: