-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstatic.cpp
More file actions
38 lines (32 loc) · 1.08 KB
/
static.cpp
File metadata and controls
38 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;
int i = 1; // i 为全局变量,具有静态生存期
int main()
{
static int a; // 静态局部变量,有全局寿命,局部可见
int b = -10; // b, c 为局部变量,具有动态生存期
int c = 0;
void other();
cout << "---MAIN---\n";
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl; // 1 0 -10 0
c = c + 8;
other(); // 33 4 0 15
cout << "---MAIN---\n";
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl; // 33 0 -10 8
i = i + 10;
other(); // 75 6 4 15
other(); // 107 8 6 15
return 0;
}
void other()
{
static int a = 2;
static int b; // a,b为静态局部变量,具有全局寿命,局部可见,只第一次进入函数时被初始化
int c = 10; // c为局部变量,具有动态生存期,每次进入函数都初始化
a = a + 2;
i = i + 32;
c = c + 5;
cout << "---OTHER---\n";
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
b = a;
}