C++中语句块与作用域

注意语句块和作用域。

如果把最中间的那一对大括号去掉,则出现重定义编译错误,由此可见大括号在这里起的作用!

(编译环境是CentOS6.3下的g++)

#include<iostream>
using namespace std;

int main( )
{
  {
    int x=1;
    cout<<x<<" The first output!"<<endl; //输出1
    {
    int x=2;
    cout<<x<<" The second output!"<<endl;  //输出2
    {
      int x=3;
      cout<<x<<" The third output!"<<endl;  //输出3
    }
    cout<<x<<" The fourth output!"<<endl;  //输出2
    }
    cout<<x<<" The fifth output!"<<endl;  //输出1
  }
  return 0;
}

 

posted @ 2018-04-14 21:46  月夜_1  阅读(183)  评论(0)    收藏  举报