c++作用域规则

//该程序演示了全局变量、自动局部变量和static局部变量的作用域问题
#include <iostream.h>

void a( void );
void b( void );
void c( void );

int x = 1;

int main()
{
 int x = 5;

 cout << "local x in outer scope of main is " << x << endl;

 {
  int x = 7;
  cout << "local x in inner scope of main is " << x << endl;
 }

 cout << "local x in outer scope of main is " << x << endl;

 a();
 b();
 c();
 a();
 b();
 c();

 cout << "local x in main is " << x << endl;

 return 0;
}

void a( void )
{
 int x = 25;
 cout << endl << "local x in a is " << x << " after entering a" << endl;

 ++x;
 cout << "local x in a is " << x << " before exiting a" << endl;
}

void b( void )
{
 static int x = 50;

 cout << endl << "local static x is " << x << " on entering b" << endl;
 ++x;
 cout << "local static x is " << x << " on exiting b" << endl;
}

void c( void )
{
 cout << endl << "global x is " << x << " on entering c" << endl;
 x *= 10;
 cout << "global x is " << x << " on exiting c" << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值