Storage classes

1,377 views 15 slides Jan 21, 2015
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Here is the introduction and brief description about storage classes


Slide Content

STORAGE CLASSES
http://computerprogramminginclanguage.blogspot.com/

STORAGE CLASSES
Introduction
Types of storage classes
Scope, visibility and lifetime of storage classes

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

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.

TYPES OF STORAGE CLASSES
Automatic Variables
External Variables
Static Variables
Register Variables

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

AUTOMATIC VARIABLES
CONTINUE…
void main()
{
 auto int i;
 printf(“ i: %d”,i);
}
Output: GarbageValue(Unexpected Value)

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

EXTERAL VARIABLES CONTINUE…
int i;
extern int i;
void main()
{
 printf(“ i: %d”,i);
}
Output: 0

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

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

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.

REGISTER VARIABLES CONTINUE…
Default initial values is garbage value.
Scope is local to the block .
Lifetime of variable is with in the block.

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*/

Storage
Class
Keyword StorageDefault
initial
value
Scope Lifetime
Automaticauto memory Garbage Local to
the block
With in the block
Externalextern memory Zero Global Till the end of
program exe
Static static memory Zero Local Value of the
variable persists
b/w diff. function
calls
RegisterregisterCPU
register
Garbage Local to
the block
With in the block
OVERVIEW