Professional Documents
Culture Documents
Programming (CC-211-3)
Instructor: Abdullah Javed
(muhammad.abdullah@pujc.edu.pk)
Visiting Lecturer,
Punjab University, Jhelum Campus.
Lecture 04.2
Fall 2021
TODAY’S AGENDA
• Variable Storage Classes
• Memory Storage Layout
• Storage Classes in Multiple Files
• Storage Classes for Functions
2
Variable Storage Classes
• Storage class of a variable determines its:
• Scope
• Lifetime
3
Scope and Storage
• C variable v has scope and storage attributes
• The storage attribute states, how long v lives
• The scope attributes states, where v is visible
• Storage attributes can be static, auto, or register
• Scope attributes can be local, global, or external:
• Local means, v is only visible inside the current, innermost scope,
independent of storage attribute; e.g. there are local static variables in C
• Global means, v is visible in the whole compilation unit, from the line of
declaration to the end of file
• External means, v is visible in this and in other compilation units, and its
storage attribute is static
4
auto Storage Class
• auto is the default storage class for a variable defined inside a
function body or a statement block
• The auto prefix is optional; i.e. any locally declared variable is
automatically auto unless specifically defined to be static
Example:
{
auto double x; /* Same as: double x */
int num; /* Same as: auto int num; */
. . .
}
5
Cont.
• Storage for an auto variable is:
• Allocated when control enters the variable’s containing block
• Released when control leaves its containing block
6
Cont.
• If an auto variable is defined but not initialized:
• Variable has an unknown value when control enters its containing block
• If an auto variable is defined and initialized at the same time:
• Variable is re-initialized each time control enters its containing block
• An auto variable’s scope is limited to its containing block (i.e., it is
local to the block)
7
static Storage Class
•Storage for a static variable:
• Is allocated when execution begins
• Exists for as long as the program is running
•A static variable may be defined either inside or outside a function’s
body.
•The static prefix must be included
Example:
static double seed;
8
Cont.
• If a static variable is defined but not initialized:
• Is set to zero (0) once, when storage is allocated
• If a static variable is simultaneously defined and initialized:
• Is initialized once, when storage is allocated
• A static variable defined inside a function body is visible only in its
containing block
• A static variable defined outside a function body is visible to all blocks
which follow it in the current compilation units
• If you wish it to be visible in other compilation units, it must be
declared extern
9
Example
#include <stdio.h>
12
Cont.
• If an extern variable is defined but not initialized:
• Is set to zero (0) once, when storage is allocated
13
Example
#include <stdio.h>
disp();
return 0;
}
14
Memory Storage Layout
Where are auto, static, and extern variables stored?
Code Segment Contains the program’s machine code
(Text Segment)
Data Segment Contains static data (e.g., static class, extern globals)
free Unallocated memory that the stack and heap can use
16
Cont.
• An extern variable is defined exactly once in a file by placing it outside all blocks.
• If an extern variable is not initialized at definition time
→ extern prefix must be omitted
Example:
extern int k;
17
Filename: ex1_A.c
#include <stdio.h>
Filename: ex1_B.c
18
Storage Classes for Functions
•extern is the default storage class for a function.
•The extern prefix is usually omitted from the function’s definition.
•An extern function can be invoked by any other function in any file.
•If a function is invoked in one file but is defined in a different file, then
declare its prototype in the file where the function is invoked.
19
Filename: ex2_A.c
#include <stdio.h>
return 0;
}
20
continued … this code is still part of ex2_A.c
{
int a; /* Local to this block in fun1 */
a = 5;
printf("4:fun1: a= %d\n", a);
}
printf("5:fun1: a= %d\n", a);
}
21
Filename: ex2_B.c
#include <stdio.h>
22
Cont.
Compile the source files using gcc:
gcc ex2_A.c ex2_B.c or gcc *.c
Actual output:
1:main: a= 2
2:fun1: x= 2
3:fun1: a= 123
4:fun1: a= 5
5:fun1: a= 123
6:fun2: a= 123
7:fun2: a= -99
8:main: a= 2
9:main: a= 99
23