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
说明:
静态局部变量存储在全局区,全局变量、静态局部变量、静态全局变量都在静态存储区分配空间。