0% found this document useful (0 votes)
171 views10 pages

PowerPoint Slides To Chapter 08

Uploaded by

Shreya Dwivedi
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)
171 views10 pages

PowerPoint Slides To Chapter 08

Uploaded by

Shreya Dwivedi
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/ 10

Program Structure

Chapter 8
Introduction
• Concerned with the structure of programs having more
than one function
• Two different ways to characterise variables:
– 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

Scope  Portion of the program over which a variable is recognised

Types of Storage Classes and Keywords to Identify


Automatic (keyword auto) External (keyword extern)
Static (keyword static) Register (keyword register)
Automatic Variables
• Always declared within a function and their scope is
confined to that function
• These variables defined in different functions will,
therefore, be independent of one another, even though they
may have the same name (not good programming practice)
• Default declaration (variables discussed till now are
automatic variables)
• Need not include the auto keyword
• Can be assigned initial values by including appropriate
expressions within variable declaration or by explicit
assignment expression selsewhere in the function
• Does not retain its value once control is transferred out of
its defining function
• Scope can be smaller than the entire function ( if declared
within a single compound statement)
External (Global) Variables
• Are not confined to a single function
• Scope extends from the point of the definition through the
remainder of the program; hence, called global variables
• Provide the convenient mechanism for transferring
information back and forth between functions
• Can be assigned initial values as a part of variable
definition, but initial values must be expressed as constants
rather then expressions
– These initial values will be assigned only once, at the beginning of the
program. External values will retain these initial values unless they are
later altered during program execution.
• If initial value not assigned then 0 value is assigned
automatically
• Arrays can be declared as external variables or automatic,
but automatic arrays cannot be initialized
• One must distinguish between external-variable definition and
external-variable declaration
Static Variables
• Defined in a single-file program (discussed next) within
individual functions and, therefore, have the same scope as
automatic variables ( cannot be accessed outside their
definition)
– The variable declaration must begin with the static storage–class
designation
• Retain their values throughout the life of the program
• If defined by the same name as automatic variable then the
local variables will take precedence over the external variable,
– though the value of the external variable will be unaffected by any manipulation
of the local variable
– External variables maintain their independence from locally defined variables
and static variables
• Initial values can be included with the same rules as that of
external variables
Multi-File Programs
• A file is a collection of information stored as a separate
entity within the storage (primary or secondary) storage
device
• Till now programs considered were single-file programs;
however, C programs are also composed of multiple files
• Allow greater flexibility in defining the scope of both
functions and variables
• Rules are more complicated
Functions
• Function definition may be either external or static
– External functions will be recognized throughout the entire program
– Static functions are recognized only within the files in which they are defined
• Storage class is established by placing appropriate storage-
class designation at the beginning of the function definition
– Else the function is assumed external if a storage-class definition does not
appear
Functions

• A function can be defined in one file and accessed in other;


the later file must include a function defined.
• To execute a multi-file program, each individual file must be
compiled and resulting object files linked together.
– To do so, combine the source files within a project.
• If a file contains a static function, it may be necessary to
include the storage class static within the function declared
or the function prototype.
Variables
• External variables can be defined in one file and
accessed in another.
• To access such variables, the variables must to
declared within another file also,
– usually placed at the beginning of the file, ahead of the first
function; definition difference here also.
• Values 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.
• Within a file, an external variable can be defined as
static.

You might also like