#include <iostream>
using namespace std;
int a = 1;
int main() {
int a = 2;
{
cout << "块变量1: " << a << endl;
int a = 3; // 块作用域变量
cout << "块变量2: " << a << endl;
cout << "块变量3: " << ::a << endl;
}
cout << "外部变量: " << a << endl;
return 0;
}
运行结果:
块变量1: 2
块变量2: 3
块变量3: 1
外部变量: 2