2.3.6

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;
}

發佈了22 篇原創文章 · 獲贊 1 · 訪問量 3062
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章