0% found this document useful (0 votes)
56 views

Programming Fundamentals: Ms. Mamoona Tasadduq Mamoona - Tasadduq@cuilahore - Edu.pk

This document discusses storage classes in C programming. It explains automatic, static, and external storage classes and how they determine an object's lifetime, scope, and linkage. Automatic variables are created and destroyed within a block. Static variables exist for the entire program execution. External variables have file scope and are known in any function. The document also provides examples demonstrating how variables with different storage classes behave.

Uploaded by

nouman ashiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Programming Fundamentals: Ms. Mamoona Tasadduq Mamoona - Tasadduq@cuilahore - Edu.pk

This document discusses storage classes in C programming. It explains automatic, static, and external storage classes and how they determine an object's lifetime, scope, and linkage. Automatic variables are created and destroyed within a block. Static variables exist for the entire program execution. External variables have file scope and are known in any function. The document also provides examples demonstrating how variables with different storage classes behave.

Uploaded by

nouman ashiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

COMSATS University

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

Automatic storage is a means of conserving memory,


because automatic variables exist only when they are
needed. They are created when the function in which
they are defined is entered and they are destroyed
when the function is exited.

3
Software Engineering Observation 5.10

Automatic storage is an example of the principle of least


privilege—allowing access to data only when it is
absolutely needed. Why have variables stored in
memory and accessible when in fact they are not
needed?

4
Performance Tip 5.2

The storage-class specifier register can be placed


before an automatic variable declaration to suggest that the
compiler maintain the variable in one of the computer’s
high-speed hardware registers. If intensely used variables
such as counters or totals can be maintained in hardware
registers, the overhead of repeatedly loading the variables
from memory into the registers and storing the results back
into memory can be eliminated.

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

Defining a variable as global rather than local allows


unintended side effects to occur when a function that
does not need access to the variable accidentally or
maliciously modifies it. In general, use of global
variables should be avoided except in certain situa­tions
with unique performance requirements (as discussed in
Chapter 14).

8
Software Engineering Observation 5.12

Variables used only in a particular function should be


defined as local variables in that function rather than as
external variables.

9
Common Programming Error 5.13

Using multiple storage-class specifiers for an identifier.


Only one storage-class specifier can be applied to an
identifier.

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

Accidentally using the same name for an identifier in an


inner block as is used for an identifier in an outer block,
when in fact you want the identifier in the outer block to
be active for the duration of the inner block.

13
Error-Prevention Tip 5.2

Avoid variable names that hide names in outer scopes.


This can be accomplished simply by avoiding the use of
duplicate identifiers in a program.

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 useLocal is 25 after entering useLocal


local x in useLocal is 26 before exiting useLocal

local static x is 50 on entering useStaticLocal


local static x is 51 on exiting useStaticLocal

global x is 1 on entering useGlobal


global x is 10 on exiting useGlobal

local x in useLocal is 25 after entering useLocal


local x in useLocal is 26 before exiting useLocal

local static x is 51 on entering useStaticLocal


local static x is 52 on exiting useStaticLocal

global x is 10 on entering useGlobal


global x is 100 on exiting useGlobal

local x in main is 5

18

You might also like