int main() { int sum=0; for(int i=0;i<10;i++) sum+=i; cout<<i<<endl; //error:只能在for语句中使用,不能在main函数的其他地方使用,具有语句作用域 system("pause"); } -------------------------------------------------------------------------------------------- std::string s1 = "hello"; // s1 has global scope int main() { std::string s2 = "world"; // s2 has local scope // uses global s1; prints ``hello world'' std::cout << s1 << " " << s2 << std::endl; //全局作用域中的名字刻在局部作用域中使用 int s1 = 42; // s1 is local and hides global s1 // uses local s1; prints ``42 world'' std::cout << s1 << " " << s2 << std::endl; return 0; }