3. INTRODUCTION
To declare and define a variable we need to
specify the datatype. But , to fully declare a
variable, it is also important to specify the
storage class.
If we don’t specify a storage class of a variable in
it’s declaration, the compiler will assume a
storage class depending on the context of the
variable is used. i.e., each variable has a specified
default storage class
Variables are generally stored in 2 locations
Memory
CPU registers
4. INTRODUCTION CONTINUE…
By Using Storage class we come to know the
following
Where the variable would be stored.
What will be the initial value of the variable, if the
initial value not specified then what is the default
value.
What is the scope of the variable, i.e., in which
functions the value of the variable would be
available.
What is the life time of the variable, i.e., how long
would be the variable exist.
6. AUTOMATIC VARIABLES
Automatic variables are declared inside a
function(block) in which they are used.
Keyword used to declare a automatic variable is
auto .
Auto variables are stored in memory.
Default initial values is garbage value.
Scope is local to the block/function in which they
are declared.
Lifetime of auto variable is till the end of the
function in which the variable is defined
These are also referred as internal or local
variables
8. EXTERNAL VARIABLES
External variables are declared outside all
functions, so that they are available to all the
functions.
Keyword used to declare external variable is
extern.
Extern variables are stored in memory.
Default initial value is zero.
Scope is global
Lifetime of extern variable is as long as the
program is executed.
These are also referred as global variables
10. STATIC VARIABLES
Static variables can declared either internal or
external .
Keyword used to declare static variable is static.
Extern variables are stored in memory.
Default initial values is zero.
Scope is global/local based on the declaration.
Lifetime of internal static variable is local to the
function or external static variables is as long as
the program is executed.
Static variables can be initialized only once
11. STATIC VARIABLES CONTINUE…
void main()
{
void increment();
increment();
increment();
increment();
}
void increment()
{
static int i=1; //int I or auto int I;
printf(“ i: %d”,i);
i++;
}
Output: i=1 i=2 i=3
12. REGISTER VARIABLES
Register variables can declared inside a
function .
Keyword used to declare static variable is
register.
Extern variables are stored in CPU registers.
Register access is much faster than a memory
access, by keeping frequently accessed variables
like looping variable(iteration) in the register
leads to faster execution of a program.
13. REGISTER VARIABLES CONTINUE…
Default initial values is garbage value.
Scope is local to the block .
Lifetime of variable is with in the block.
14. REGISTER VARIABLES CONTINUE…
void main()
{
register int i;
for(i=1;i<=5;i++)
printf(“ i: %d”,i);
}
Output: i:1 i:2 i:3 i:4 i:5
/* Not sure the variable is stored in register,
because it is limited*/
15. Storage
Class
Keyword Storage Default
initial
value
Scope Lifetime
Automatic auto memory Garbage Local to
the block
With in the block
External extern memory Zero Global Till the end of
program exe
Static static memory Zero Local Value of the
variable persists
b/w diff. function
calls
Register register CPU
register
Garbage Local to
the block
With in the block
OVERVIEW