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

来源:互联网 发布:一键安装php集成环境 编辑:程序博客网 时间:2024/05/16 18:22

运行下面一段程序后,变量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声明的变量的作用域扩大了。