Short Notes: Storage Classes | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


STORAGE CLASSES
To completely define a variable one needs to mention its type along with its storage class. In 
other words we can say not only variables have a data type but also they have ‘storage classes.
Till now the storage class of any variable is not mentioned because storage classes have defaults. 
If someone doesn’t specify the storage class of a variable during its declaration, the compiler 
assumes a storage class depending upon the situation in which the variable is going to be used. 
Therefore we can now say every variable have certain default storage class.
Compiler identifies the physical location within the computer where the string of bits which 
represents the variable’s values are stored from the variable name itself.
Generally there are two kinds of locations in a computer where such a value can be present, these 
are Memory and CPU registers. The storage class of a particular variable determines in which of 
the above two locations the variable’s value is stored.
There are four properties by which storage class of a variable can be recognized.These are scope, 
default initial value, scope and life.
A variable’s storage class reveals the following things about a variable
(i) Where the variable is stored.
(ii) What is the initial value of the variable if the value of the variable is not specified?
(iii) What is the scope of the variable (To which function or block the variable is available).
(iv) What is the life of particular variable (Up to what extent the variable exists in a program).
There are four storage classes in C, these are Automatic, Static, Register and External. The 
keywords auto, static, registers and exter are used for the above storage classes respectively.
We can specify a storage class while declaring a variable. The
general syntax is
storageclass datatype variablename;
Page 2


STORAGE CLASSES
To completely define a variable one needs to mention its type along with its storage class. In 
other words we can say not only variables have a data type but also they have ‘storage classes.
Till now the storage class of any variable is not mentioned because storage classes have defaults. 
If someone doesn’t specify the storage class of a variable during its declaration, the compiler 
assumes a storage class depending upon the situation in which the variable is going to be used. 
Therefore we can now say every variable have certain default storage class.
Compiler identifies the physical location within the computer where the string of bits which 
represents the variable’s values are stored from the variable name itself.
Generally there are two kinds of locations in a computer where such a value can be present, these 
are Memory and CPU registers. The storage class of a particular variable determines in which of 
the above two locations the variable’s value is stored.
There are four properties by which storage class of a variable can be recognized.These are scope, 
default initial value, scope and life.
A variable’s storage class reveals the following things about a variable
(i) Where the variable is stored.
(ii) What is the initial value of the variable if the value of the variable is not specified?
(iii) What is the scope of the variable (To which function or block the variable is available).
(iv) What is the life of particular variable (Up to what extent the variable exists in a program).
There are four storage classes in C, these are Automatic, Static, Register and External. The 
keywords auto, static, registers and exter are used for the above storage classes respectively.
We can specify a storage class while declaring a variable. The
general syntax is
storageclass datatype variablename;
The following table shows different properties of a variable which according to its storage class.
^ \ ^ Properties
Storage
Class
Storage Default Initial 
Value
Scope Life
Automatic Memory Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Register CPU
Registers
Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Static Memory Zero Local to the block 
in which the 
variable is defined
Value of the variable 
continues to exist 
between different 
function calls
External Memory Zero Global Till the program’s 
execution doesn’t 
come to an end
Automatic Storage Class
Syntax to declare automatic variable is:
auto datatype variablename;
Example:
auto int i;
Features of Automatic Storage Class are as follows 
Storage: Memory
Page 3


STORAGE CLASSES
To completely define a variable one needs to mention its type along with its storage class. In 
other words we can say not only variables have a data type but also they have ‘storage classes.
Till now the storage class of any variable is not mentioned because storage classes have defaults. 
If someone doesn’t specify the storage class of a variable during its declaration, the compiler 
assumes a storage class depending upon the situation in which the variable is going to be used. 
Therefore we can now say every variable have certain default storage class.
Compiler identifies the physical location within the computer where the string of bits which 
represents the variable’s values are stored from the variable name itself.
Generally there are two kinds of locations in a computer where such a value can be present, these 
are Memory and CPU registers. The storage class of a particular variable determines in which of 
the above two locations the variable’s value is stored.
There are four properties by which storage class of a variable can be recognized.These are scope, 
default initial value, scope and life.
A variable’s storage class reveals the following things about a variable
(i) Where the variable is stored.
(ii) What is the initial value of the variable if the value of the variable is not specified?
(iii) What is the scope of the variable (To which function or block the variable is available).
(iv) What is the life of particular variable (Up to what extent the variable exists in a program).
There are four storage classes in C, these are Automatic, Static, Register and External. The 
keywords auto, static, registers and exter are used for the above storage classes respectively.
We can specify a storage class while declaring a variable. The
general syntax is
storageclass datatype variablename;
The following table shows different properties of a variable which according to its storage class.
^ \ ^ Properties
Storage
Class
Storage Default Initial 
Value
Scope Life
Automatic Memory Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Register CPU
Registers
Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Static Memory Zero Local to the block 
in which the 
variable is defined
Value of the variable 
continues to exist 
between different 
function calls
External Memory Zero Global Till the program’s 
execution doesn’t 
come to an end
Automatic Storage Class
Syntax to declare automatic variable is:
auto datatype variablename;
Example:
auto int i;
Features of Automatic Storage Class are as follows 
Storage: Memory
Default Initial Value: Garbage Value
Scope: Local to the block in which the variable is defined
Life: Till the control remains within the block in which the variable is defined
All the variables which are declared inside a block or function without any storage class specifier 
are automatic variables. So by default every variable is automatic variable. We can use auto 
keyword to declare automatic variables but generally it is not done. Since automatic variables are 
known inside a function or block only, we may have variables of same name in different 
functions or blocks without any doubt. Look at the following two functions:
void fun1()
{
int x,y;
/* Some statements*/ 
}
void fun1()
{
auto int x,y;
/*Some statements*/
}
In the above two functions declaration statements are equivalent as both declare variables x and y 
as automatic variables.
The following program illustrates the work of automatic variables.
void test(); 
void main()
{
test();
test();
test();
}
void test()
{
auto int k=10;
printf(“%d\n” ,k);
k++;
}
Page 4


STORAGE CLASSES
To completely define a variable one needs to mention its type along with its storage class. In 
other words we can say not only variables have a data type but also they have ‘storage classes.
Till now the storage class of any variable is not mentioned because storage classes have defaults. 
If someone doesn’t specify the storage class of a variable during its declaration, the compiler 
assumes a storage class depending upon the situation in which the variable is going to be used. 
Therefore we can now say every variable have certain default storage class.
Compiler identifies the physical location within the computer where the string of bits which 
represents the variable’s values are stored from the variable name itself.
Generally there are two kinds of locations in a computer where such a value can be present, these 
are Memory and CPU registers. The storage class of a particular variable determines in which of 
the above two locations the variable’s value is stored.
There are four properties by which storage class of a variable can be recognized.These are scope, 
default initial value, scope and life.
A variable’s storage class reveals the following things about a variable
(i) Where the variable is stored.
(ii) What is the initial value of the variable if the value of the variable is not specified?
(iii) What is the scope of the variable (To which function or block the variable is available).
(iv) What is the life of particular variable (Up to what extent the variable exists in a program).
There are four storage classes in C, these are Automatic, Static, Register and External. The 
keywords auto, static, registers and exter are used for the above storage classes respectively.
We can specify a storage class while declaring a variable. The
general syntax is
storageclass datatype variablename;
The following table shows different properties of a variable which according to its storage class.
^ \ ^ Properties
Storage
Class
Storage Default Initial 
Value
Scope Life
Automatic Memory Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Register CPU
Registers
Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Static Memory Zero Local to the block 
in which the 
variable is defined
Value of the variable 
continues to exist 
between different 
function calls
External Memory Zero Global Till the program’s 
execution doesn’t 
come to an end
Automatic Storage Class
Syntax to declare automatic variable is:
auto datatype variablename;
Example:
auto int i;
Features of Automatic Storage Class are as follows 
Storage: Memory
Default Initial Value: Garbage Value
Scope: Local to the block in which the variable is defined
Life: Till the control remains within the block in which the variable is defined
All the variables which are declared inside a block or function without any storage class specifier 
are automatic variables. So by default every variable is automatic variable. We can use auto 
keyword to declare automatic variables but generally it is not done. Since automatic variables are 
known inside a function or block only, we may have variables of same name in different 
functions or blocks without any doubt. Look at the following two functions:
void fun1()
{
int x,y;
/* Some statements*/ 
}
void fun1()
{
auto int x,y;
/*Some statements*/
}
In the above two functions declaration statements are equivalent as both declare variables x and y 
as automatic variables.
The following program illustrates the work of automatic variables.
void test(); 
void main()
{
test();
test();
test();
}
void test()
{
auto int k=10;
printf(“%d\n” ,k);
k++;
}
Output:
10
10
10
In the above program when the function test() is called for the first time ,variable k is created and 
initialized to 10. When the control returns to main(), k is destroyed. When function test() is 
called for the second time again k is created , initialized and destroyed after execution of the 
function. Hence automatic variables came into existence each time the function is executed and 
destroyed when execution of the function completes.
Register Storage Class
Syntax to declare register variable is:
register datatype variablename;
Features of Register Storage Class are as follows:
Storage: CPU Registers
Default Initial Value: Garbage Value
Scope: Local to the block in which the variable is defined
Life: Till the control remains within the block in which the variable is defined
Register storage class can be applied only to automatic variables. Its scope, life and default initial 
value are same as that of automatic variables. The only difference between register and automatic 
variables is the place where they are stored. Automatic variables are stored in memory but 
register variables are stored in CPU registers. Registers are small storage units present in the 
processor. The variables stored in registers can be accessed much faster than the variables stored 
in registers. Hence the variables which are frequently used in a program can be assigned register 
storage class for faster execution. For example loop counters are declared as register variables, 
which are defined as follows:
int main()
{
register int a; 
for(a=0;i<50000;i++) 
printf(“ %d\t” ,a); 
return 0;
}
Page 5


STORAGE CLASSES
To completely define a variable one needs to mention its type along with its storage class. In 
other words we can say not only variables have a data type but also they have ‘storage classes.
Till now the storage class of any variable is not mentioned because storage classes have defaults. 
If someone doesn’t specify the storage class of a variable during its declaration, the compiler 
assumes a storage class depending upon the situation in which the variable is going to be used. 
Therefore we can now say every variable have certain default storage class.
Compiler identifies the physical location within the computer where the string of bits which 
represents the variable’s values are stored from the variable name itself.
Generally there are two kinds of locations in a computer where such a value can be present, these 
are Memory and CPU registers. The storage class of a particular variable determines in which of 
the above two locations the variable’s value is stored.
There are four properties by which storage class of a variable can be recognized.These are scope, 
default initial value, scope and life.
A variable’s storage class reveals the following things about a variable
(i) Where the variable is stored.
(ii) What is the initial value of the variable if the value of the variable is not specified?
(iii) What is the scope of the variable (To which function or block the variable is available).
(iv) What is the life of particular variable (Up to what extent the variable exists in a program).
There are four storage classes in C, these are Automatic, Static, Register and External. The 
keywords auto, static, registers and exter are used for the above storage classes respectively.
We can specify a storage class while declaring a variable. The
general syntax is
storageclass datatype variablename;
The following table shows different properties of a variable which according to its storage class.
^ \ ^ Properties
Storage
Class
Storage Default Initial 
Value
Scope Life
Automatic Memory Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Register CPU
Registers
Garbage Value Local to the block 
in which the 
variable is defined
Till the control 
remains within the 
block in which the 
variable is defined
Static Memory Zero Local to the block 
in which the 
variable is defined
Value of the variable 
continues to exist 
between different 
function calls
External Memory Zero Global Till the program’s 
execution doesn’t 
come to an end
Automatic Storage Class
Syntax to declare automatic variable is:
auto datatype variablename;
Example:
auto int i;
Features of Automatic Storage Class are as follows 
Storage: Memory
Default Initial Value: Garbage Value
Scope: Local to the block in which the variable is defined
Life: Till the control remains within the block in which the variable is defined
All the variables which are declared inside a block or function without any storage class specifier 
are automatic variables. So by default every variable is automatic variable. We can use auto 
keyword to declare automatic variables but generally it is not done. Since automatic variables are 
known inside a function or block only, we may have variables of same name in different 
functions or blocks without any doubt. Look at the following two functions:
void fun1()
{
int x,y;
/* Some statements*/ 
}
void fun1()
{
auto int x,y;
/*Some statements*/
}
In the above two functions declaration statements are equivalent as both declare variables x and y 
as automatic variables.
The following program illustrates the work of automatic variables.
void test(); 
void main()
{
test();
test();
test();
}
void test()
{
auto int k=10;
printf(“%d\n” ,k);
k++;
}
Output:
10
10
10
In the above program when the function test() is called for the first time ,variable k is created and 
initialized to 10. When the control returns to main(), k is destroyed. When function test() is 
called for the second time again k is created , initialized and destroyed after execution of the 
function. Hence automatic variables came into existence each time the function is executed and 
destroyed when execution of the function completes.
Register Storage Class
Syntax to declare register variable is:
register datatype variablename;
Features of Register Storage Class are as follows:
Storage: CPU Registers
Default Initial Value: Garbage Value
Scope: Local to the block in which the variable is defined
Life: Till the control remains within the block in which the variable is defined
Register storage class can be applied only to automatic variables. Its scope, life and default initial 
value are same as that of automatic variables. The only difference between register and automatic 
variables is the place where they are stored. Automatic variables are stored in memory but 
register variables are stored in CPU registers. Registers are small storage units present in the 
processor. The variables stored in registers can be accessed much faster than the variables stored 
in registers. Hence the variables which are frequently used in a program can be assigned register 
storage class for faster execution. For example loop counters are declared as register variables, 
which are defined as follows:
int main()
{
register int a; 
for(a=0;i<50000;i++) 
printf(“ %d\t” ,a); 
return 0;
}
In the above program, variable a was used frequently as a loop counter so the variable a is 
defined as a register variable. Register is a not a command but just a request to the compiler to 
allocate the memory for the variable into the register. If free registers are available than memory 
will be allocated in to the registers. And if there are no free registers then memory will be 
allocated in the RAM only (Storage is in memory i.e. it will act as automatic variable).
Every type of variables can be stored in CPU registers. Suppose the microprocessor has 16 bit 
registers then they can’t hold a float or a double value which requires 4 and 8 bytes respectively. 
But if you use register storage class for a float or double variable then you will not get any error 
message rather the compiler will treat the float and double variable as be of automatic storage 
class(i.e. Will treat them like automatic variables).
Static Storage Class
Syntax to declare static variable is:
static datatype variablename;
Example:
static int i;
Features of Static Storage Class are as follows 
Storage: Memory 
Default Initial Value: Zero
Scope: Local to the block in which the variable is defined
Life: Value of the variable continues to exist between different function calls
Now look at the previous program with k is declared as static instead of automatic.
void test(); 
void main()
{
test();
test();
test();
}
void test()
{
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

study material

,

Viva Questions

,

pdf

,

Short Notes: Storage Classes | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Short Notes: Storage Classes | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Objective type Questions

,

video lectures

,

MCQs

,

mock tests for examination

,

past year papers

,

Previous Year Questions with Solutions

,

Exam

,

Free

,

Semester Notes

,

Important questions

,

practice quizzes

,

Short Notes: Storage Classes | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

ppt

,

Summary

,

Sample Paper

,

shortcuts and tricks

;