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

Chapter 8 program structure

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)
6 views

Chapter 8 program structure

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/ 28

Program Structure

Chapter 8

1
Objectives
 To cover the structure of programs consisting of more than one
function.
 To learn the distinction between “local” variables that are
recognized only within a single function, and “global” variables
that are recognized in two or more functions.
 We will see how global variables are defined and utilized in this
chapter.
 We will also consider the issue of static vs. dynamic retention of
information by a local variable. That is, a local variable normally
does not retain its value once control has been transferred out of
its defining function. In some circumstances, however, it may be
desirable to have certain local variables retain their values, so
that the function can be reentered at a later time and the
computation resumed.
 To develop a large, multifunction program in terms of several
independent files, with a small number of functions (perhaps
only one) defined within each file. In such programs the
individual functions can be defined and accessed locally within a
2single file, or globally within multiple files.
STORAGE CLASSES

 There are two different ways to characterize


variables: by data type, and by storage class.
 Data type refers to the type of information
represented by a variable, e.g., integer number,
floating-point number, character, etc.
 Storage class refers to the permanence of a
variable, and its scope within the program,
i.e., the portion of the program over which
the variable is recognized.
 There are four different storage-class
specifications in C: automatic, external, static
and register.
 They are identified by the keywords auto, extern,
static , and register , respectively.
3
STORAGE CLASSES
The storage class associated with a variable can
sometimes be established simply by the location of
the variable declaration within the program.
In other situations, however, the keyword that
specifies a particular storage class must be placed
at the beginning of the variable declaration.
The exact procedure for establishing a storage
class for a variable depends upon the particular
storage class, and the manner in which the
program is organized (i.e., single file vs. multiple
file)

4
AUTOMATIC VARIABLES
Automatic variables are always declared
within a function and are local to the function
in which they are declared; that is, their scope is
confined to that function.
Automatic variables defined in different functions
will therefore be independent of one another, even
though they may have the same name.
Any variable declared within a function is
interpreted as an automatic variable unless a
different storage class specification is shown within
the declaration.

5
Example: Calculating Factorials

6
AUTOMATIC VARIABLES
An automatic variable does not retain its
value once control is transferred out of its
defining function.
Therefore, any value assigned to an automatic
variable within a function will be lost once the
function is exited.
The scope of an automatic variable can be smaller
than an entire function if we wish.
In fact, automatic variables can be declared within
a single compound statement.
With small, simple programs there is usually no
advantage in doing this, but it may be desirable in
larger programs.
7
Example: Average Length of Several Lines
of Text

8
EXTERNAL (GLOBAL)
VARIABLES
External variables, in contrast to automatic
variables, are not confined to single functions.
Their scope extends from the point of definition
through the remainder of the program.
Hence, they usually span two or more functions, and
often an entire program.
They are often referred to as global variables.
Since external variables are recognized globally, they
can be accessed from any function that falls within
their scope.
They retain their assigned values within this scope.
Therefore an external variable can be assigned a value
within one function, and this value can be used (by
accessing the external variable) within another
9
function.
VARIABLES
The use of external variables provides a
convenient mechanism for transferring information
back and forth between functions.
In particular, we can transfer information into a
function without using arguments.
This is especially convenient when a function
requires numerous input data items.
Moreover, we now have a way to transfer multiple
data items out of a function, since the return
statement can return only one data item.

10
EXTERNAL (GLOBAL) VARIABLES
 When working with external variables, we must distinguish between external
variable definitions and external variable declarations.
 An external variable definition is written in the same manner as an
ordinary variable declaration. It must appear outside of, and usually before,
the functions that access the external variables.
 An external variable definition will automatically allocate the required
storage space for the external variables within the computer’s memory.
 The assignment of initial values can be included within an external variable
definition if desired (more about this later).
 The storage-class specifier extern is not required in an external variable
definition, since the external variables will be identified by the location of
their definition within the program. In fact, many C compilers forbid the use
of extern within an external variable definition.
 If a function requires an external variable that has been defined earlier in
the program, then the function may access the external variable freely,
without any special declaration within the function. (Remember, however,
that any alteration to the value an external variable within a
function will be recognized within the entire scope of the external
variable.)
 On the other hand, if the function definition precedes the external
11 variable definition, then the function must include a declaration for that
external variable.
VARIABLES
An external variable declaration must begin
with the storage-class specifier extern. The
name of the external variable and its data type
must agree with the corresponding external
variable definition that appears outside of the
function.
Storage space for external variables will not be
allocated as a result of an external variable
declaration. Moreover, an external variable
declaration cannot include the assignment of
initial values.
These distinctions between an external variable
definition and an external variable
12
declaration are very important.
EXAMPLE

13
STATIC VARIABLES
 In a single-file program, static variables are defined within individual functions and
therefore have the same scope as automatic variables; i.e., they are local to the
functions in which they are defined.
 Unlike automatic variables, however, static variables retain their values throughout
the life of the program. Thus, if a function is exited and then re-entered at a later
time, the static variables defined within that function will retain their former values.
 This feature allows functions to retain information permanently throughout the
execution of a program.
 Static variables are defined within a function in the same manner as automatic
variables, except that the variable declaration must begin with the static storage-
class designation.
 Static variables can be utilized within the function in the same manner as other
variables. They cannot, however, be accessed outside of their defining function.
 It is not unusual to define automatic or static variables having the same names as
external variables. In such situations the local variables will take precedence over
the external variables, though the values of the external variables will be
unaffected by any manipulation of the local variables.
 Thus the external variables maintain their independence from locally defined
automatic and static variables. The same is true of local variables within one
function that have the same names as local variables within another function.

14
Example
Shown below is the skeletal structure of a C
program that includes variables belonging to
several different storage classes.

15
STATIC VARIABLES
Initial values can be included in the static variable
declarations.
In particular:
1. The initial values must be expressed as constants, not
expressions.
2. The initial values are assigned to their respective variables
at the beginning of program execution. The variables retain
these values throughout the life of the program, unless
different values are assigned during the course of the
computation.
3. Zeros will be assigned to all static variables whose
declarations do not include explicit initial values. Hence,
static variables will always have assigned values.
16
Example
Generating Fibonacci Numbers:
 The Fibonacci numbers form an interesting
sequence in which each number is equal to the
sum of the previous two numbers.
 In other words, where Fi refers to the ith
Fibonacci number. The first two Fibonacci
numbers are defined to equal 1;i.e.,
F1 =F2= 1
 Hence
 F3 =F2 + F1= 1 + 1 =2
 F4 = F3 + F2 =2 + 1 = 3
 F5 = F4 + F3 = 3 + 2 = 5
and so on.

17
18
Input-Output

19
MULTIFILE PROGRAMS
 A file is a collection of information stored as a separate
entity within the computer or on an auxiliary storage device.
 A file can be a collection of data, a source program, a portion
of a source program, an object program, etc.
 C programs that are contained entirely within a single file ,
or are composed of multiple files.
 This is especially true of programs that make use of lengthy
functions, where each function may occupy a separate file. Or, if
there are many small related functions within a program, it may be
desirable to place a few functions within each of several files.
 The individual files will be compiled separately, and then linked
together to form one executable object program .
 This facilitates the editing and debugging of the program,
since each file can be maintained at a manageable size.
 Multifile programs allow greater flexibility in defining the scope of
both functions and variables.
 The rules associated with the use of storage classes become more
complicated, however, because they apply to functions as well as
variables, and more options are available for both external and static
variables.

20
Rules for Functions
 Considering the rules associated with the use of functions.
 Within a multifile program, a function definition may be
either external or static.
 An external function will be recognized throughout
the entire program, whereas a static function will be
recognized only within the file in which it is defined.
 In each case, the storage class is established by placing
the appropriate storage-class designation (i.e., either
extern or static) at the beginning of the function
definition.
 The function is assumed to be external if a storage
class designation does not appear.
 In general terms, the first line of a function definition can
be written as
storage-class data-type name(type I arg 1, type 2 arg
2, . . . type n arg n)
21
Rules for Functions
 When a function is defined in one file and accessed in
another, the latter file must include a function
declaration. This declaration identifies the function as
an external function whose definition appears
elsewhere.
 Such declarations are usually placed at the beginning of the
file, ahead of any function definitions.
 It is good programming practice to begin the declaration with
the storage-class specifier extern.
 In general terms, a function declaration can be written as
 storage - class data - type name( argument type 7,
argument type 2, . . . , argument type n ) ;
 A function declaration can also be written using full function
prototyping as
storage-class data-type name(typeI argI, type2
arg2, . ..typen argn);
22
EXAMPLE
Here is a simple program that generates the
message "Hello, there!" from within a
function. The program consists of two
functions: main and output. Each function
appears in a separate file.

23
Program Code

24
Rules for Variables
 Within a multifile program, external (global) variables can be defined in one file and
accessed in another.
 An external variable definition can appear in only one file.
 Usually, it will appear at the beginning of the file, ahead of the first function
definition.
 External variable definitions may include initial values. Any external variable that is
not assigned an initial value will automatically be initialized to zero.
 The storage-class specifier extern is not required within the definition; in fact,
many versions of C specifically forbid the appearance of this storage-class specifier in
external variable definitions. Thus, external variable definitions are
recognized by their location within the defining files and by their appearance.
 In order to access an external variable in another file, the variable must first be
declared within that file.
 This declaration may appear anywhere within the file. Usually, however, it will be
placed at the beginning of the file, ahead of the first function definition. The
declaration must begin with the storage-class specifier extern. Initial values
cannot be included in external variable declarations.
 The value assigned to an external variable may be altered within any file in which the
variable is recognized. Such changes will be recognized in all other files that
fall within the scope of the variable.
 Thus, external variables provide a convenient means of transferring information
25 between files.
Example
Shown below is a skeletal outline of a two-file C
program that makes use of external variables.

26
Portability of C
 During the process of converting a C source program into an executable
object program, the compiled source program may be linked with one or
more library files to produce the final executable program.
 Thus, the final program may be assembled from two or more separate
files, even though the original source program may have been contained
within a single file.
 The required header files must be merged with the source program
during the compilation process.
 This is accomplished by placing one or more #include statements at the
beginning of the source program (or at the beginning of the individual
program files).
 Platform independence (i.e., machine independence) is a significant
advantage in this approach to the use of library functions and header
files. Thus, machine-dependent features can be provided as library
functions, or as character constants or macros that are included
within the header files.
 A typical C program will therefore run on many different kinds of
computers without 'alteration, provided the appropriate library functions
and header files are utilized. The portability resulting from this approach
is a major contributor to the popularity of C.
27
Thank You!!!

28

You might also like