1.全局区
include <iostream>
using namespace std;
//全局变量
int c=10;
int d=10;
//const修饰的全局变量 全局常量
const int g=10;
const int h=10;
int main()
{
//全局区
//全局变量、静态变量、常量
//创建普通局部变量
int a = 10;
int b = 10;
//局部变量a和b的地址
cout<<"局部变量a的地址是:"<<(long int)&a<<endl;
cout<<"局部变量b的地址是:"<<(long int)&b<<endl;
//全局变量c和d的地址
cout<<"全局变量c的地址是:"<<(long int)&c<<endl;
cout<<"全局变量d的地址是:"<<(long int)&d<<endl;
//静态变量 在普通变量前面加static,属于静态变量
static int e=10;
static int f=10;
//静态变量地址
cout<<"静态变量e的地址是:"<<(long int)&e<<endl;
cout<<"静态变量f的地址是:"<<(long int)&f<<endl;
//常量
//字符串常量
cout<<"字符串常量的地址是:"<<(long int)&"hello world"<<endl;
//const修饰的变量
//const修饰的全局变量
cout<<"const修饰的全局变量的地址是:"<<(long int)&g<<endl;
cout<<"const修饰的全局变量的地址是:"<<(long int)&h<<endl;
//const修饰的局部变量
const int c_l_a=10;//c-const g-global l-local
const int c_l_b=10;
//const修饰的局部变量的地址
cout<<"const修饰的局部变量的地址是:"<<(long int)&c_l_a<<endl;
cout<<"const修饰的局部变量的地址是:"<<(long int)&c_l_b<<endl;
return 0;
}
代码输出结果:
局部变量a的地址是:6158891096
局部变量b的地址是:6158891092
全局变量c的地址是:4308025344
全局变量d的地址是:4308025348
静态变量e的地址是:4308025352
静态变量f的地址是:4308025356
字符串常量的地址是:4308008537
const修饰的全局变量的地址是:4308008636
const修饰的全局变量的地址是:4308008640
const修饰的局部变量的地址是:6158891088
const修饰的局部变量的地址是:6158891084