Documente Academic
Documente Profesional
Documente Cultură
Goutam Majumder
September 7, 2018
1 Introduction
Scope
The scope of an identifier is a part of the program in which the identifier
can be used to access its object. There are different types of scope: block
(or local), function, function prototype, and file. These categories depend
on how and where identifiers are declared.
Block
– The scope of an identifier with block (or local) scope starts at the dec-
laration point and ends at the end of the block containing the declaration
(such block is known as the enclosing block).
– Parameter declarations with a function definition also have block scope,
limited to the scope of the function body.
File
File scope identifiers, also known as globals, are declared outside of all
blocks; their scope is from the point of declaration to the end of the source
file.
Function
The only identifiers having function scope are statement labels. Label names
can be used with goto statements anywhere in the function in which the
label is declared. Labels are declared implicitly by writing label name fol-
lowed by a statement. Label names must be unique within a function.
Function Prototype
Identifiers declared within the list of parameter declarations in a function
prototype (not as a part of a function definition) have a function prototype
scope. This scope ends at the end of the function prototype.
Goutam Majumder (LPU) Short title September 7, 2018 5 / 20
Visibility and Scope of a variable
File Scope
An identifier whose declaration is located outside any block or function
parameter list has file scope. An identifier with file scope is visible from the
declaration of the identifier to the end of the compilation unit, unless hidden
by an inner block declaration. In the following example, the identifier off
has file scope:
Function Scope
– Only statement labels have function scope. An identifier with function
scope is unique throughout the function in which it is declared.
– Labeled statements are used as targets for goto statements and are im-
plicitly declared by their syntax, which is the label followed by a colon ( )
and a statement. For example:
In this example, the identifiers (david, susan, mary , and john ) have scope
beginning at their declarations and ending at the closing parenthesis. The
type of the function students is ”function returning int with four int pa-
rameters.” In effect, these identifiers are merely placeholders for the actual
parameter names to be used after the function is defined.
Introduction
These specifiers tell the compiler how to store the subsequent variable. The
general form of a variable declaration that uses a storage class is shown
here:
storage class specifier data type variable name;
Points to be Remember
At most one storage class specifier may be given in a declaration. If no
storage class specifier is specified then following rules are used: – Variables
declared inside a function are taken to be auto.
– Functions declared within a function are taken to be extern.
– Variables and functions declared outside a function are taken to be static,
with external linkage.
Points to be remember
– Variables and functions having external linkage are available to all files
that constitute a program.
– File scope variables and functions declared as static (described shortly)
have internal linkage.
– These are known only within the file in which they are declared.
– Local variables have no linkage and are therefore known only within their
own block.
static as local
– When static specifier is applied to a local variable inside a function or
block, the compiler creates permanent storage for it, much as it creates
storage for a global variable but static local variable remains visible only to
the function or block in which it is defined.
– In simple terms, a static local variable is a local variable that retains its
value between function calls.
Goutam Majumder (LPU) Short title September 7, 2018 15 / 20
Types of Storage Class
Example on Static (as local) Storage Class
The following program code defines static variable i at two places in two
blocks inside function staticDemo(). Function staticDemo() is called twice
within from main function. During second call static variables retain their
old values and they are not initialized again in second call of staticDemo().
Goutam Majumder (LPU) Short title September 7, 2018 16 / 20
Types of Storage Class
Static Specifier
static as global
When static specifier is applied to a global variable or a function then com-
piler makes that variable or function known only to the file in which it is
defined. A static global variable has internal linkage that means even though
the variable is global; routines in other files have no knowledge of it and
cannot access and alter its contents directly.