SlideShare a Scribd company logo
1 of 14
BY,
A.Amalajoylet
Storage classes
STORAGE CLASSES
 The storage class determines the part of the
memory where the variables would be stored.
 The storage class also determines the initial
value of the variable.
 It is used to define the scope and lifetime of
variable.
 There are two storage locations in computer,
 1. CPU Registers
 2.Memory.
 A value stored in CPU register can always be
accessed faster than the one that is stored in
memory.
Four types
1.Automatic storage class.
2.Register storage class.
3.Static storage class.
4.External storage class.
Automatic
 Keywords : Auto
 Storage : Memory
 Default initial value : Garbage value
 Scope : Local/Block scope
 Life : Exists as long as
the control
remains in the
block
Example for Automatic
 void main()
{
auto x= 20 ;
{
auto x = 60 ;
printf("n x: %d",x);
}
printf("nx: %d",x);
}
 OUTPUT :
 x: 60
x: 20
 NOTE : Two variables are declared in different
blocks , so they are treated as different variables
Static
 Keywords : Static
 Storage : Memory
 Default initial value : 0
 Scope : Local to the block in
which
the variable is
defined.
 Life : Value of the variable
persists
between different
function calls.
Difference between auto and static
class
 /*auto class*/
#include<stdio.h>
void add();
int main()
{
add();
add();
add();
add();
return 0;
}
void add()
{
auto int i=1;
printf("n%d",i);
i=i+1;
 OUTPUT:-
1
1
1
1
 /*static class*/
#include<stdio.h>
void add();
int main()
{
add();
add();
add();
add();
return 0;
}
void add()
{
static int i=1;
printf("n%d",i);
i=i+1;
}
 OUTPUT:-
1
2
3
4

Previous program explanation
 If the storage class is static, then the statement
static int i = 1 is executed only once, irrespective
of how many times the same function is called.
 If the storage class is auto,then the statement
auto int i=1 is executed each time where it
increments and re-initialize the value of i= 1.
 The difference between them is
that static variable do not disappear when the
function is no longer active. There value persist. If
control comes back to the same function again ,
the static variables have the same values they
had last time around.
Register
 Keywords : Register.
 Storage : CPU register.
 Default initial value : Garbage value
 Scope : Local to the block in
which
the variable is
defined.
 Life : Value of the variable
persists
till the control
remains within
the block in which
Why we need Register Variable
?
 Whenever we declare any variable inside C Program
then memory will be randomly allocated at particular
memory location.
 We have to keep track of that memory location. We
need to access value at that memory location using
ampersand operator/Address Operator i.e (&).
 If we store same variable in the register memory then
we can access that memory location directly without
using the Address operator.
 Register variable will be accessed faster than the
normal variable thus increasing the operation and
program execution. Generally we use register variable
as Counter.
 Note : It is not applicable for arrays, structures or
pointers.
External
 Keywords : Extern.
 Storage : Memory.
 Default initial value : 0
 Scope : Global
 Life : Exists as long as
variable is
running
Retains value within
the function
Example for Extern
 int num = 75 ;
void display();
void main()
{
extern int num ;
printf("nNum : %d",num);
display();
}
void display()
{
extern int num ;
printf("nNum : %d",num);
}
 OUTPUT:
 Num : 75
Num : 75
Extern contd…
Note :Declaration within the function indicates
that the function uses external variable
Functions belonging to same source code ,
does not require declaration (no need to write
extern)
If variable is defined outside the source code ,
then declaration using extern keyword is
required
 THANK YOU

More Related Content

What's hot

What's hot (20)

Function in c program
Function in c programFunction in c program
Function in c program
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C functions
C functionsC functions
C functions
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Templates
TemplatesTemplates
Templates
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 

Similar to Storage class

What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
DaisyWatson5
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 

Similar to Storage class (20)

Storage class
Storage classStorage class
Storage class
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Storage classes
Storage classesStorage classes
Storage classes
 
STORAGE CLASSES
STORAGE CLASSESSTORAGE CLASSES
STORAGE CLASSES
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes
Storage classesStorage classes
Storage classes
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming Notes
 
Storage class
Storage classStorage class
Storage class
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Storage Class Specifiers
Storage Class SpecifiersStorage Class Specifiers
Storage Class Specifiers
 
memory
memorymemory
memory
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Terms and Definitions.pdf
Terms and Definitions.pdfTerms and Definitions.pdf
Terms and Definitions.pdf
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 

Recently uploaded

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
chumtiyababu
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 

Storage class

  • 2. STORAGE CLASSES  The storage class determines the part of the memory where the variables would be stored.  The storage class also determines the initial value of the variable.  It is used to define the scope and lifetime of variable.  There are two storage locations in computer,  1. CPU Registers  2.Memory.  A value stored in CPU register can always be accessed faster than the one that is stored in memory.
  • 3. Four types 1.Automatic storage class. 2.Register storage class. 3.Static storage class. 4.External storage class.
  • 4. Automatic  Keywords : Auto  Storage : Memory  Default initial value : Garbage value  Scope : Local/Block scope  Life : Exists as long as the control remains in the block
  • 5. Example for Automatic  void main() { auto x= 20 ; { auto x = 60 ; printf("n x: %d",x); } printf("nx: %d",x); }  OUTPUT :  x: 60 x: 20  NOTE : Two variables are declared in different blocks , so they are treated as different variables
  • 6. Static  Keywords : Static  Storage : Memory  Default initial value : 0  Scope : Local to the block in which the variable is defined.  Life : Value of the variable persists between different function calls.
  • 7. Difference between auto and static class  /*auto class*/ #include<stdio.h> void add(); int main() { add(); add(); add(); add(); return 0; } void add() { auto int i=1; printf("n%d",i); i=i+1;  OUTPUT:- 1 1 1 1  /*static class*/ #include<stdio.h> void add(); int main() { add(); add(); add(); add(); return 0; } void add() { static int i=1; printf("n%d",i); i=i+1; }  OUTPUT:- 1 2 3 4 
  • 8. Previous program explanation  If the storage class is static, then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.  If the storage class is auto,then the statement auto int i=1 is executed each time where it increments and re-initialize the value of i= 1.  The difference between them is that static variable do not disappear when the function is no longer active. There value persist. If control comes back to the same function again , the static variables have the same values they had last time around.
  • 9. Register  Keywords : Register.  Storage : CPU register.  Default initial value : Garbage value  Scope : Local to the block in which the variable is defined.  Life : Value of the variable persists till the control remains within the block in which
  • 10. Why we need Register Variable ?  Whenever we declare any variable inside C Program then memory will be randomly allocated at particular memory location.  We have to keep track of that memory location. We need to access value at that memory location using ampersand operator/Address Operator i.e (&).  If we store same variable in the register memory then we can access that memory location directly without using the Address operator.  Register variable will be accessed faster than the normal variable thus increasing the operation and program execution. Generally we use register variable as Counter.  Note : It is not applicable for arrays, structures or pointers.
  • 11. External  Keywords : Extern.  Storage : Memory.  Default initial value : 0  Scope : Global  Life : Exists as long as variable is running Retains value within the function
  • 12. Example for Extern  int num = 75 ; void display(); void main() { extern int num ; printf("nNum : %d",num); display(); } void display() { extern int num ; printf("nNum : %d",num); }  OUTPUT:  Num : 75 Num : 75
  • 13. Extern contd… Note :Declaration within the function indicates that the function uses external variable Functions belonging to same source code , does not require declaration (no need to write extern) If variable is defined outside the source code , then declaration using extern keyword is required