码迷,mamicode.com
首页 > 编程语言 > 详细

C++@语句块

时间:2015-05-30 19:44:11      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
using namespace std;

int main()
{
	{
		int x=1;
		cout << x << endl;
		{
			cout << x << endl;
			int x=2;
			cout << x <<endl;
			{
				cout << x <<endl;
				int x=3;
				cout << x <<endl;
			}
			cout << x <<endl;
		}
		cout << x << endl;
	}
	return 0;
}

输出结果 1 1 2 2 3 2 1

#include <iostream>
using namespace std;

int main()
{
	{
		int x1=1;
		cout << x1 << endl;
		{
			cout << x1 << endl;
			int x2=2;
			cout << x2 <<endl;
			{
				cout << x2 <<endl;
				int x3=3;
				cout << x3 <<endl;
			}
			cout << x2 <<endl;
		}
		cout << x1 << endl;
	}
	return 0;
}

输出结果 1 1 2 2 3 2 1 

/*
* 在第一例每个语句块中,虽然变量名都一样,但是int x= ;之后x就变成另外一个变量了
* 且生存周期仅限于当前的语句块中
*/

C++@语句块

标签:

原文地址:http://www.cnblogs.com/kwseeker-bolgs/p/4540791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!