Programming Fundamentals: Ms. Mamoona Tasadduq Mamoona - Tasadduq@cuilahore - Edu.pk
Programming Fundamentals: Ms. Mamoona Tasadduq Mamoona - Tasadduq@cuilahore - Edu.pk
Islamabad
(Lahore Campus)
Programming
Fundamentals
Ms. Mamoona Tasadduq
[email protected]
5.12 Storage Classes
•Storage class specifiers
• Storage duration – how long an object exists in memory
• Scope – where object can be referenced in program
• Linkage – specifies the files in which an identifier is known (more in Chapter 14)
•Automatic storage
• Object created and destroyed within its block
• auto: default for local variables
auto double x, y;
• register: tries to put variable into high-speed registers
• Can only be used for automatic variables
register int counter = 1;
2
Performance Tip 5.1
3
Software Engineering Observation 5.10
4
Performance Tip 5.2
5
Performance Tip 5.3
Often, register declarations are unnecessary.
Today’s optimizing compilers
are capable of recognizing frequently used variables
and can decide to place them in registers without the
need for a register declaration.
6
5.12 Storage Classes
• Static storage
• Variables exist for entire program execution
• Default value of zero
• static: local variables defined in functions.
• Keep value after function ends
• Only known in their own function
• extern: default for global variables and functions
• Known in any function
7
Software Engineering Observation 5.11
8
Software Engineering Observation 5.12
9
Common Programming Error 5.13
10
5.13 Scope Rules
• File scope
• Identifier defined outside function, known in all
functions
• Used for global variables, function definitions, function
prototypes
• Function scope
• Can only be referenced inside a function body
• Used only for labels (start:, case: , etc.)
11
5.13 Scope Rules
• Block scope
• Identifier declared inside a block
• Block scope begins at definition, ends at right brace
• Used for variables, function parameters (local variables of
function)
• Outer blocks "hidden" from inner blocks if there is a
variable with the same name in the inner block
• Function prototype scope
• Used for identifiers in parameter list
12
Common Programming Error 5.14
13
Error-Prevention Tip 5.2
14
Outline
fig05_12.c
1 /* Fig. 5.12: fig05_12.c
2 A scoping example */ (1 of 4 )
3 #include <stdio.h>
4
5 void useLocal( void ); /* function prototype */
6 void useStaticLocal( void ); /* function prototype */
7 void useGlobal( void ); /* function prototype */
8
9 int x = 1; /* global variable */
10 Global variable with file scope
11 /* function main begins program execution */
12 int main( void )
13 {
14 int x = 5; /* local variable to main */ Variable with block scope
15
16 printf("local x in outer scope of main is %d\n", x );
17
18 { /* start new scope */
19 int x = 7; /* local variable to new scope */ Variable with block scope
20
21 printf( "local x in inner scope of main is %d\n", x );
22 } /* end new scope */
23 15
Outline
24 printf( "local x in outer scope of main is %d\n", x );
25
26
fig05_12.c
useLocal(); /* useLocal has automatic local x */
27 useStaticLocal(); /* useStaticLocal has static local x */
28 useGlobal(); /* useGlobal uses global x */
(2 of 4 )
29 useLocal(); /* useLocal reinitializes automatic local x */
30 useStaticLocal(); /* static local x retains its prior value */
31 useGlobal(); /* global x also retains its value */
32
33 printf( "\nlocal x in main is %d\n", x );
34
35 return 0; /* indicates successful termination */
36
37 } /* end main */
38
39 /* useLocal reinitializes local variable x during each call */
40 void useLocal( void )
41 {
42 int x = 25; /* initialized each time useLocal is called */
43
Variable with blo
44 printf( "\nlocal x in useLocal is %d after entering useLocal\n", x );
45 x++;
46 printf( "local x in useLocal is %d before exiting useLocal\n", x );
47 } /* end function useLocal */
48
16
Outline
fig05_12.c
49 /* useStaticLocal initializes static local variable x only the first time
(3 of 4 )
50 the function is called; value of x is saved between calls to this
51 function */
52 void useStaticLocal( void )
53 {
54 /* initialized only first time useStaticLocal is called */
55 static int x = 50;
56 Static variable with block scope
57 printf( "\nlocal static x is %d on entering useStaticLocal\n", x );
58 x++;
59 printf( "local static x is %d on exiting useStaticLocal\n", x );
60 } /* end function useStaticLocal */
61
62 /* function useGlobal modifies global variable x during each call */
63 void useGlobal( void )
64 {
65 printf( "\nglobal x is %d on entering useGlobal\n", x );
66 x *= 10;
67 printf( "global x is %d on exiting useGlobal\n", x ); Global variable
68 } /* end function useGlobal */
17
Outline
fig05_12.c
local x in outer scope of main is 5
local x in inner scope of main is 7 (4 of 4 )
local x in outer scope of main is 5
local x in main is 5
18