您的位置:首页 > 编程语言 > C语言/C++

C语言中的静态局部变量

2016-05-09 23:32 197 查看
代码:

0x601070 0x7ffcf44243fc
0x60106c
1
0x60106c
2
0x60106c
3
[hu@localhost test]$ cat test.cpp
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

void func(){
static int a = 1;
cout<<&a<<endl;
cout<<a<<endl;
a++;
}

int main(){
static int a = 1;
int b = 1;
cout<<&a<<" "<<&b<<endl;
func();
func();
func();

return 0;
}


输出:

0x601070 0x7ffcf44243fc
0x60106c
1
0x60106c
2
0x60106c
3


说明:

静态局部变量存储在全局区,全局变量、静态局部变量、静态全局变量都在静态存储区分配空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: