Tuesday, November 2, 2010

STORAGE CLASSES IN C


STORAGE CLASSES


        Every C variable has a storage class and a scope. The storage class determines the part of memory where storage is allocated for an object and how long the storage allocation continues to exist. It also determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name.
There are following storage classes which can be used in a C Program
  • auto
  • register
  • static
  • extern

auto - Storage Class

auto is the default storage class for all local variables.
               {
            int Count;
            auto int Month;
               }
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

register - Storage Class

register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).
               {
            register int  Miles;
               }
Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implementation restrictions.

static - Storage Class

static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.
               static int Count;
        int Road;

        {
            printf("%d\n", Road);
        }
static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.
static can also be defined within a function. If this is done the variable is initialised at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during various calls.
   void func(void);
  
   static count=10; /* Global variable - static is the default */
  
   main()
   {
     while (count--)
     {
         func();
     }
  
   }
  
   void func( void )
   {
     static i = 5;
     i++;
     printf("i is %d and count is %d\n", i, count);
   }
  
   This will produce following result
  
   i is 6 and count is 9
   i is 7 and count is 8
   i is 8 and count is 7
   i is 9 and count is 6
   i is 10 and count is 5
   i is 11 and count is 4
   i is 12 and count is 3
   i is 13 and count is 2
   i is 14 and count is 1
   i is 15 and count is 0
NOTE : Here keyword void means function does not return anything and it does not take any parameter. You can memorise void as nothing. static variables are initialized to 0 automatically.
Example:
main()
{
   value();
   value ();
   value();
   getch();
}

value()
{
   static int a=5;
   a=a+2;
   printf("\n%d",a);
}

The output of the program is not 7,7,7 but it is, 
7,9,11  

Definition vs Declaration: Before proceeding, let us understand the difference between definition and declaration of a variable or function. Definition means where a variable or function is defined in reality and actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function. Through declaration we assure to the complier that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples char *func(void) has been put at the top which is a declaration of this function where as this function has been defined below to main() function.
There is one more very important use for 'static'. Consider this bit of code.
   char *func(void);

   main()
   {
      char *Text1;
      Text1 = func();
   }

   char *func(void)
   {
      char Text2[10]="martin";
      return(Text2);
   }
Now, 'func' returns a pointer to the memory location where 'text2' starts BUT text2 has a storage class of 'auto' and will disappear when we exit the function and could be overwritten but something else. The answer is to specify
    static char Text[10]="martin";
The storage assigned to 'text2' will remain reserved for the duration if the program.

extern - Storage Class

extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to declare a global variable or function in other files.
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 %i\n", count);
   }
Here extern keyword is being used to declare count in another file.
Now compile these two files as follows
   gcc main.c write.c -o write
This fill produce write program which can be executed to produce result.
Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value
In briefly a variable storage class tells us 
1.    Where the variable would be stored.
2.     What will be the initial value of the variable, if the initial value is not specifically assigned (i.e, the default initial value).
3.     What is the scope of the variable, i.e., in which functions the value of the variable would be available.
4.     What is the life of the variables; i.e., how long would the variable exist.

No comments: