Storage Class
Storage Class
O Level / A Level
Storage Class
A storage class defined the scope, visibility and a life time(extent) of a variable.
Scope of a Variable
The scope of a variable determines over what part(s) of the program a variable is actually
available for use (active).
Longevity: It refers to the period during which a variables retains a given value during
execution of a program (alive).
Local (internal) variables are those which are declared within a particular function.
Global (external) variables are those which are declared outside any function.
Scope of a Declaration
Scope of a declaration is the region of C program text over which that declaration is active.
Toplevel identifiers – Extends from declaration point to end of file.
Formal parameters in functions – Extends from declaration point to end of function
body.
Block/function (local) identifiers – Extends from declaration point to end of
block/function.
Automatic variables
Are declared inside a function in which they are to be utilized.
Are declared using a keyword auto. eg. auto int number;
Are created when the function is called and destroyed automatically when the function is
exited. These variables are private (local) to the function in which they are declared.
Variables declared inside a function without storage class specification is, by
default, an automatic variable.
Auto is the default storage class for all local variables.
{
int count;
auto int month;
}
The example above defines two variables with the same storage class.
Auto can only be used within functions, i.e. local variables.
During recursion, the nested variables are unique auto variables
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);
}
The features of automatic variables are
Storage – memory.
Default initial value an unpredictable value, which is often a garbage value.
Scope local to the block in which the variable is defined.
Life till the control remains within the block variable is defined.
External Variables
These variables are declared outside any function.
These variables are active and alive throughout the entire program.
Also known as global variables and default value is zero.
Unlike local variables they can be accessed by any function in the program.
In case local variable and global variable have the same name, the local variable will have
precedence over the global one.
Sometimes the keyword extern is used to declare these variables.
Extern is used to give a reference of a global variable that is visible to all the program files.
It is visible only from the point of declaration to the end of the program.
The extern variable cannot be initialized, as all it does is, point the variable name at a
storage location that has been previously defined.
When a programmer has multiple files and defines a global variable or function, which
will be used in other files also, then extern will be used in another file to give reference of
defined variable or function.
Extern is used to declare a global variable or function in another file.