-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 6.5 (Completed)
118 lines (100 loc) · 3.79 KB
/
Exercise 6.5 (Completed)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
1. (Practice) a. List the storage categories available to local variables.
The storage categories available to local variables are auto, static, and register.
b. List the storage categories available to global variables.
The storage categories available to global variables are extern and static. A local auto
variable is unique to the function in which it’s declared. Every time the function is called,
the auto variable is re-created, as though it never existed. A local static variable is also
unique to the function where it’s declared. However, a static variable retains its last value
and isn’t re-created when its function is called again.
2. (Practice) Describe the difference between a local auto variable and a local static variable.
The first function declares yrs to be a static variable and assigns a value of 1 to it only once,
when the function is compiled. Each time the function is called thereafter, the value in yrs is
increased by 2. The second function also declares yrs to be static but assigns it the value 1
every time it’s called, and the value of yrs after the function is finished will always be 3. By
resetting the value of yrs to 1 each time it’s called, the second function defeats the purpose
of declaring the variable to be static.
3. (Practice) What’s the difference between the following functions?
void init1()
{
static int yrs = 1;
cout << “The value of yrs is “ << yrs << endl;
yrs = yrs + 2;
return;
}
void init2()
{
static int yrs;
yrs = 1;
cout << “The value of yrs is “ << yrs << endl;
yrs = yrs + 2;
return;
}
*The difference between the following functions is that the yrs's value is declared at slightly different times.*
4. (Practice) a. Describe the difference between a global static variable and a global extern
variable.
A global static variable are initalized to 0 at complie time.
The global extern variable is used to extend a global variable's scope beyond its normal boundaries.
b. If a variable is declared with an extern storage category, what other declaration statement
must be present somewhere in the program?
An extern declaration statement simply informs the computer that a global
variable already exists and can now be used. The actual storage for the variable must be created
somewhere else in the program by using one, and only one, global declaration statement
in which the keyword extern hasn’t been used.
5. (Practice) The declaration statement static double years; can be used to create a local
or global static variable. What determines the scope of the variable years?
The scope of the variable years is static and can only be extended to the scope of the file only.
6. (Practice) For the function and variable declarations shown in Figure 6.15, place an extern
declaration to accomplish each of the following:
a. Extend the scope of the global variable choice into file2. Done!!
b. Extend the scope of the global variable flag into the average() function only. Done!!
c. Extend the scope of the global variable date into average() and variance(). Done!!
d. Extend the scope of the global variable date into roi() only. Done!!
e. Extend the scope of the global variable coupon into roi() only. Done!!
f. Extend the scope of the global variable bondtype into file1. Done!!
g. Extend the scope of the global variable maturity into both watts() and thrust(). Done!!
file1
extern char choice;
int flag;
long date, time;
int main()
{
.
.
.
}
double factor;
double watts()
{
.
exten int maturity;
}
double thrust()
{
.
static int maturity;
.
}
-------------------------------
file2
char bondtype;
double resistance;
exten double roi()
{
exten int coupon;
static int coupon;
exten long data;
static long data;
}
double average()
{
exten long data;
static int flag;
.
}
double variance()
{
static long data;
.
.
}
*See textbook Figure 6.15 Files for Exercise 6