c++入门教程–-12作用域

135 阅读1分钟

1在函数或块内部的局部变量
2在所有函数外部的全局变量

#include <iostream>
using namespace std; 
int b;//全局变量,整个代码所有位置都可以用

int main ()
{
  /* 局部变量声明,只有在main函数可以用 */
  int a;
 
  cout<<a<<endl; 
cout<<b<<endl; 

  return 0;
}

int Do()
{
int c; //局部变量,只能在Do函数中使用
c=2;
return c;

}