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

Lecture 13 - Storage Classes

  • 1.
    Storage Classes Md. ImranHossain Showrov (showrovsworld@gmail.com) 13 1
  • 2.
    Outline  What isStorage 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 StorageClasses?  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 inC  We have four different storage classes in a C program  auto  register  static  extern
  • 5.
  • 6.
    The auto StorageClass  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 StorageClass (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 StorageClass (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 StorageClass (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 StorageClass (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 StorageClass (cont..) Output: sum is : 5
  • 12.
    What is GlobalVariable?  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 StorageClass 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 StorageClass (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 StorageClass (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 StorageClass  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 StorageClass (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 StorageClass 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 StorageClass 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 StorageClass  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 StorageClass (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; }