Storage Classes in C
Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.
2. Introduction
Variables declared in C programs are
different than other programming
languages.
Can use the same variable names in the
C program in separate blocks.
When we declare a variable,it is
available only to a specific part or block
of the program.
Remaining block or other functions
cannot access the variable.
Variables declared within functions are
called local variables and those declared
outside are called external or global
variables.
3. Definition
Scope of variables:
The area or scope of the C program from
where the variable can be accessed.
The area or scope of the variable
depends on its storage class i.e. Where
and how it is declared.
The storage class of a variable tells the
compiler:
i)the storage area of the variable
ii)the initial value of the variable.
Iii)the scope of the variable.
Iv) the life of the variable i.e. How long the
variable would be active in the program.
4. Variables declared in C can have any
one of the four storage classes:
Automatic
External
Static
Register
5. Lifetime of a Variable
Every variable has a lifetime i.e. Its time
duration during which its status is active
in the program.
The time gap between its declaration
and ckeanup.
The lifetime depends upon the storage
class.
For eg,auto variable gets destroyed
immediately when the function execution
is over,whereas static variable remains in
the memory.
6. Visibility of a Variable
It defines the scope of a variable.
The scope is of two types:
◦ Local scope
◦ Global scope
Global is recognized throughout the
program,whereas the local variable scope is
limited to the declaration block
7.
8. Automatic variables
The auto keyword is applied to all
local variables automatically.
It is the default storage class that is
why it is known as automatic variable.
the scope of the variable is local to the
block in which it is defined,available
only to the current block or program.
The vanish after the block of code
finishes executing.
10. Register Variables
The register variable allocates
memory in register than RAM.
It has a faster access than other
variables.
CPU registers are limited,hence
cannot declare more register
variables.
It is recommended to use register
variable only for quick access such as
in counter.
Note: We can't get the address of
12. Static variables
The static variable is initialized only
once and exists till the end of the
program.
It retains its value between multiple
functions call.
The static variable has the default
value 0 which is provided by
compiler,initialised once,it is never
reinitialized.
Can be used to count how many times
14. External Variable
The extern variable is visible to all the
programs.
It is used if two or more files are
sharing same variable or function.
In case in aprogram where both
external and auto variables have been
declared with the same name,the first
priority is given to the auto variable.
Variable defined at other place,other
than the place where it is declared.