0% found this document useful (0 votes)
12 views15 pages

Storage Classes

Uploaded by

archanajn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Storage Classes

Uploaded by

archanajn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

STORAGE CLASSES

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*/
OVERVIEW
Storage Keywor Storag Default Scope Lifetime
Class d e initial
value
Automati auto memory Garbage Local to With in the block
c 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 Garbage Local to With in the block
register the
block

You might also like