SlideShare a Scribd company logo
Storage Classes
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
13
1
Outline
 What is Storage Classes?
 Storage Classes in C
 The auto Storage Class
 What is GlobalVariable?
 The extern Storage Class
 The static Storage Class
 The register Storage Class
What is Storage Classes?
 Storage Classes are used to describe about the
features of a variable/function.
 These features basically include the scope, visibility
and life-time which help us to trace the existence of a
particular variable during the runtime of a program.
Storage Classes in C
 We have four different storage classes in a C program
 auto
 register
 static
 extern
Storage Classes in C (cont..)
The auto Storage Class
 This is the default storage class for all the variables
declared inside a function or a block.
 Hence, the keyword auto is rarely used while writing
programs in C language.
 Auto variables can be only accessed within the
block/function they have been declared and not
outside them (which defines their scope). Of course,
these can be accessed within nested blocks within
the parent block/function in which the auto variable
was declared.
The auto Storage Class (cont..)
 However, they can be accessed outside their scope as
well using the concept of pointers given here by
pointing to the very exact memory location where
the variables resides.
 They are assigned a garbage value by default
whenever they are declared.
The auto Storage Class (cont..)
 However, they can be accessed outside their scope as
well using the concept of pointers given here by
pointing to the very exact memory location where
the variables resides.
 They are assigned a garbage value by default
whenever they are declared.
The auto Storage Class (cont..)
{
int mount;
auto int month;
}
 The example above defines two variables with in the
same storage class. 'auto' can only be used within
functions, i.e., local variables.
The auto Storage Class (cont..)
//sum of two numbers
#include <stdio.h>
int sum(int n1, int n2){
auto int s; //declaration of auto(local) variable
s = n1+n2;
return s;
}
int main(){
int i = 2, j = 3, k;
k = sum(i, j);
printf("sum is : %dn", k);
return 0;
}
The auto Storage Class (cont..)
Output:
sum is : 5
What is Global Variable?
 A global variable is a variable which is declared
outside of all the functions. It can be accessed
throughout the program and we can change its
value anytime within any function.
Global Variable Example
#include <stdio.h>
int g;
void print(){
g = 10;
printf("g = %dn", g);
}
int main(){
g = 7;
printf("g = %dn", g);
print();
return 0;
}
Global Variable Example
Output:
g = 7
g = 10
 Here, g is the global variable defined outside of all the
functions. In the main function, its value was assigned as 7 and
in the print function as 10.
The extern Storage Class using example
We can do same example by using extern keyword
as shown below.
firstfile.c
int g = 0;
In the first program file firstfile.c, we declared a global
variable g.
The extern Storage Class (cont..)
Now, we will declare this variable 'g' as extern in a
header file firstfile.h and then include it in the second
file in which we want to use this variable.
firstfile.h
extern int g;
The extern Storage Class (cont..)
Now in the second program file secondfile.c, in order to use the
global variable 'g', we need to include the header file in it by writing
#include "firstfile.h". Here we assigned a value 4 to the variable 'g'
and thus the value of 'g' in this program becomes 4.
secondfile.c
#include "firstfile.h"
main(){
g = 4;
printf("%d", g);
}
The extern …
 extern storage class simply tells us that the variable is
defined elsewhere and not within the same block
where it is used.
 Basically, the value is assigned to it in a different block
and this can be overwritten/changed in a different block
as well.
 So an extern variable is nothing but a global variable
initialized with a legal value where it is declared in
order to be used elsewhere.
 It can be accessed within any function/block.
The extern … (cont..)
 A normal global variable can be made extern as well by
placing the ‘extern’ keyword before its
declaration/definition in any function/block.
 This basically signifies that we are not initializing a new
variable but instead we are using/accessing the global
variable only.
 The main purpose of using extern variables is that they
can be accessed between two different files which are
part of a large program.
The static Storage Class
 This storage class is used to declare static variables
which are popularly used while writing programs in C
language.
 Static variables have a property of preserving their
value even after they are out of their scope! Hence,
static variables preserve the value of their last use in
their scope.
 So we can say that they are initialized only once and
exist till the termination of the program.Thus, no new
memory is allocated because they are not re-declared.
The static Storage Class (cont..)
 Their scope is local to the function to which they were
defined.
 Global static variables can be accessed anywhere in the
program. By default, they are assigned the value 0 by
the compiler.
The static Storage Class Example
#include <stdio.h>
static int g = 5;
void fn(){
static int i = 0;
printf("g = %dt", g--);
printf("i = %dn",i++);
}
int main(){
while(g >= 2)
fn();
return 0;
}
The static Storage Class Example
Output:
g = 5 i = 0
g = 4 i = 1
g = 3 i = 2
g = 2 i = 3
 Here, g and i are the static variables in which 'g' is a global variable and
'i' is a local variable. If we had not written static before the declaration
of 'i', then everytime the function 'fn()' would have been called, 'i' would
have been declared every time with an initial value of 0 and as the
function 'fn()' would exit, it would also have got destroyed.
The register Storage Class
 This storage class declares register variables which
have the same functionality as that of the auto
variables.The only difference is that the compiler
tries to store these variables in the register of the
microprocessor if a free register is available.
 This makes the use of register variables to be much
faster than that of the variables stored in the memory
during the runtime of the program.
 If a free register is not available, these are then stored
in the memory only.
The register Storage Class (cont..)
 Usually few variables which are to be accessed very frequently
in a program are declared with the register keyword which
improves the running time of the program.
 An important and interesting point to be noted here is that
we cannot obtain the address of a register variable using
pointers.
{
register int miles;
}
Lecture 13 - Storage Classes

More Related Content

PPT
Lecture 14 - Scope Rules
PPT
Lecture 11 - Functions
PPT
Lecture 9- Control Structures 1
PPTX
Storage classes in c language
PPT
Lecture 8- Data Input and Output
PPTX
Presentation on function
PPTX
Function in c
PPSX
Function in c
Lecture 14 - Scope Rules
Lecture 11 - Functions
Lecture 9- Control Structures 1
Storage classes in c language
Lecture 8- Data Input and Output
Presentation on function
Function in c
Function in c

What's hot (20)

PPT
RECURSION IN C
PPTX
C function
PPT
Lecture 10 - Control Structures 2
PPT
Recursion in c
PPTX
Functions in c language
PPSX
Functions in c
PPTX
Programming in C (part 2)
PPTX
Function in c program
PPT
Lecture 12 - Recursion
PPTX
PPT
lets play with "c"..!!! :):)
PPTX
Functions in C
PPTX
Function in c program
PPTX
Pointer basics
PPTX
C programming(part 3)
PPT
Lecture 6- Intorduction to C Programming
PPTX
C function presentation
PPTX
Function in c language(defination and declaration)
PPT
Function in C Language
PDF
Function lecture
RECURSION IN C
C function
Lecture 10 - Control Structures 2
Recursion in c
Functions in c language
Functions in c
Programming in C (part 2)
Function in c program
Lecture 12 - Recursion
lets play with "c"..!!! :):)
Functions in C
Function in c program
Pointer basics
C programming(part 3)
Lecture 6- Intorduction to C Programming
C function presentation
Function in c language(defination and declaration)
Function in C Language
Function lecture
Ad

Similar to Lecture 13 - Storage Classes (20)

DOC
What is storage class
PPT
Functions in c
DOC
Storage classess of C progamming
PPTX
Storage classes in C
DOC
5.program structure
DOCX
Storage class
PDF
Latest C Interview Questions and Answers
PPTX
Storage classes
PPTX
Storage Class Specifiers in C++
PPTX
Storage classes
PDF
Storage classes arrays & functions in C Language
PDF
Unit iii
DOC
C notes diploma-ee-3rd-sem
PDF
Data structure scope of variables
PDF
EXTERN -- wherever u define variables, it will get access to use them
PPTX
Storage Classes and Functions
PPT
Storage classes
PPTX
Storage_classes_and_Scope_rules.pptx
PDF
SPL 9 | Scope of Variables in C
What is storage class
Functions in c
Storage classess of C progamming
Storage classes in C
5.program structure
Storage class
Latest C Interview Questions and Answers
Storage classes
Storage Class Specifiers in C++
Storage classes
Storage classes arrays & functions in C Language
Unit iii
C notes diploma-ee-3rd-sem
Data structure scope of variables
EXTERN -- wherever u define variables, it will get access to use them
Storage Classes and Functions
Storage classes
Storage_classes_and_Scope_rules.pptx
SPL 9 | Scope of Variables in C
Ad

More from Md. Imran Hossain Showrov (14)

PPT
Lecture 22 - Error Handling
PPT
Lecture 21 - Preprocessor and Header File
PPT
Lecture 20 - File Handling
PPT
Lecture 19 - Struct and Union
PPT
Lecture 18 - Pointers
PPT
Lecture 16 - Multi dimensional Array
PPT
Lecture 17 - Strings
PPT
Lecture 15 - Array
PPT
Lecture 7- Operators and Expressions
PPT
Lecture 5 - Structured Programming Language
PPT
Lecture 4- Computer Software and Languages
PPT
Lecture 3 - Processors, Memory and I/O devices
PPT
Lecture 2 - Introductory Concepts
PPT
Lecture 1- History of C Programming
Lecture 22 - Error Handling
Lecture 21 - Preprocessor and Header File
Lecture 20 - File Handling
Lecture 19 - Struct and Union
Lecture 18 - Pointers
Lecture 16 - Multi dimensional Array
Lecture 17 - Strings
Lecture 15 - Array
Lecture 7- Operators and Expressions
Lecture 5 - Structured Programming Language
Lecture 4- Computer Software and Languages
Lecture 3 - Processors, Memory and I/O devices
Lecture 2 - Introductory Concepts
Lecture 1- History of C Programming

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
Sports Quiz easy sports quiz sports quiz
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
TR - Agricultural Crops Production NC III.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Renaissance Architecture: A Journey from Faith to Humanism
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf

Lecture 13 - Storage Classes

  • 1. Storage Classes Md. Imran Hossain Showrov (showrovsworld@gmail.com) 13 1
  • 2. Outline  What is Storage Classes?  Storage Classes in C  The auto Storage Class  What is GlobalVariable?  The extern Storage Class  The static Storage Class  The register Storage Class
  • 3. What is Storage Classes?  Storage Classes are used to describe about the features of a variable/function.  These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.
  • 4. Storage Classes in C  We have four different storage classes in a C program  auto  register  static  extern
  • 5. Storage Classes in C (cont..)
  • 6. The auto Storage Class  This is the default storage class for all the variables declared inside a function or a block.  Hence, the keyword auto is rarely used while writing programs in C language.  Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared.
  • 7. The auto Storage Class (cont..)  However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides.  They are assigned a garbage value by default whenever they are declared.
  • 8. The auto Storage Class (cont..)  However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides.  They are assigned a garbage value by default whenever they are declared.
  • 9. The auto Storage Class (cont..) { int mount; auto int month; }  The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.
  • 10. The auto Storage Class (cont..) //sum of two numbers #include <stdio.h> int sum(int n1, int n2){ auto int s; //declaration of auto(local) variable s = n1+n2; return s; } int main(){ int i = 2, j = 3, k; k = sum(i, j); printf("sum is : %dn", k); return 0; }
  • 11. The auto Storage Class (cont..) Output: sum is : 5
  • 12. What is Global Variable?  A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function.
  • 13. Global Variable Example #include <stdio.h> int g; void print(){ g = 10; printf("g = %dn", g); } int main(){ g = 7; printf("g = %dn", g); print(); return 0; }
  • 14. Global Variable Example Output: g = 7 g = 10  Here, g is the global variable defined outside of all the functions. In the main function, its value was assigned as 7 and in the print function as 10.
  • 15. The extern Storage Class using example We can do same example by using extern keyword as shown below. firstfile.c int g = 0; In the first program file firstfile.c, we declared a global variable g.
  • 16. The extern Storage Class (cont..) Now, we will declare this variable 'g' as extern in a header file firstfile.h and then include it in the second file in which we want to use this variable. firstfile.h extern int g;
  • 17. The extern Storage Class (cont..) Now in the second program file secondfile.c, in order to use the global variable 'g', we need to include the header file in it by writing #include "firstfile.h". Here we assigned a value 4 to the variable 'g' and thus the value of 'g' in this program becomes 4. secondfile.c #include "firstfile.h" main(){ g = 4; printf("%d", g); }
  • 18. The extern …  extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used.  Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well.  So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere.  It can be accessed within any function/block.
  • 19. The extern … (cont..)  A normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block.  This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only.  The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
  • 20. The static Storage Class  This storage class is used to declare static variables which are popularly used while writing programs in C language.  Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope.  So we can say that they are initialized only once and exist till the termination of the program.Thus, no new memory is allocated because they are not re-declared.
  • 21. The static Storage Class (cont..)  Their scope is local to the function to which they were defined.  Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
  • 22. The static Storage Class Example #include <stdio.h> static int g = 5; void fn(){ static int i = 0; printf("g = %dt", g--); printf("i = %dn",i++); } int main(){ while(g >= 2) fn(); return 0; }
  • 23. The static Storage Class Example Output: g = 5 i = 0 g = 4 i = 1 g = 3 i = 2 g = 2 i = 3  Here, g and i are the static variables in which 'g' is a global variable and 'i' is a local variable. If we had not written static before the declaration of 'i', then everytime the function 'fn()' would have been called, 'i' would have been declared every time with an initial value of 0 and as the function 'fn()' would exit, it would also have got destroyed.
  • 24. The register Storage Class  This storage class declares register variables which have the same functionality as that of the auto variables.The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available.  This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program.  If a free register is not available, these are then stored in the memory only.
  • 25. The register Storage Class (cont..)  Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program.  An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers. { register int miles; }