Handbook of Computer Science(cs) and IT

Storage Classes in C

A variable name identifies some physical location within the computer, where the string of bits representing the variable’s value, is stored.

There are basically two kinds of  locations in a computer,_where such a value may be kept

(i) Memory                                                                            (ii) CPU registers

It is the variable’s storage class that determines in which of the above two types of locations, the value should be stored.

We have four types of storage classes in C

  1. Auto 2. Register 3. Static                                                 4. Extern

Auto  Storage Class

Features of this class are given below

  • Storage Location 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 variable is defined.

Auto is the default storage class for all local variables.

void main ( )

{

auto int =3;

auto int j;

}

 

Register Storage Class

 

Register is used to define local variables that should be stored in a register instead of RAM. Register should only be used for variables that require quick access such as counters. Features of register storage class are given below

  • Storage Location CPU register
  • Default Initial Value Garbage value
  • Scope Local to the block in which variable is defined.
  • Life Till the control remains within the block in which the variable is

void main ( )

{

register int i ;

}

 

Static Storage Class

 

Static is the default storage class for global variables. Features of static storage class are given below

  • Storage Location Memory
  • Default Initial Value Zero
  • Scope Local to the block in which the variable is defined. In case of global variable, the scope will be through out the program. •
  • Life Value of variable persists between different function calls.

# i nclude <stdio.h>                                                                           #i nclude <stdio.h>

void increment ( ) ;                                                   void increment ( )

void main ( )                                                 void main ( )

{                                                                              {

increment ( ) ;                                           increment ( ) ;

increment ( ) ;                                                           increment ( ) ;

}                                                                                       }

void increment ( )                                                               void increment ( )

{                                                                                              {

auto int =1;                                                                     static int i = 1;

printf (“%d \n”, i );                               printf (“%d \n”, 1);

i=i+ 1;                                                                  i=i+1;

}                                                                          }

Output 1                                                                Output 1

2                                                              2

Extern Storage Class

Extern is used of give a reference of a global variable that is variable to a the program files, When we use extern, the variable can’t be initialized as all, it does, is to point the variable name at a storage location that has been

previously defined.

  • Storage Location Memory
  • Default Initial Value Zero
  • Scope Global
  • Life As long as the program’s execution does not come to an end.

extern int y; * declaration*/

int y = 3 ;                      *definition*/

Features of extern storage class are given below

File 1 main.c

int count = 5;

main ( )

{

write extern ( );

}

File 2 write. c

void write_extern (void);

extern int count;

void write_extern (void)

{

printf (“count is %d \n”, count);

}

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

Leave a Reply

Your email address will not be published. Required fields are marked *