时间: 2020-11-23|52次围观|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就变成另外一个变量了
* 且生存周期仅限于当前的语句块中
*/

转载于:https://www.cnblogs.com/kwseeker-bolgs/p/4540791.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/97575132

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《C++@语句块
   

还没有人抢沙发呢~