C/C++变量作用域

函数作用域

作用域标识变量在哪里创建,在哪里销毁。简单来讲作用域由变量所在的最近的一对花括号确定

以下为示例代码,使用不在作用域的变量会引起编译错误

#include <iostream>

int main()
{
	using namespace std;
	int scp1 = 1;
	cout << "scp1 = " << scp1 << endl;
	{
		int scp2  =2;
		cout << "scp1 = " << scp1 << " scp2 = "<< scp2 << endl;
		{
			int scp3 = 3;
			
			/*内层作用域 隐藏外层作用域*/
			int scp2  = 500;
			
			cout << "scp1 = " << scp1 << " scp2 = "<< scp2 << " scp3 = " << scp3 << endl;
		}//scp3 destroyed here
	}//scp2 destroyed here
	return 0;
}//scp1 destroyed here

你可能感兴趣的:(UNIX/LINUX,C)