Monday 31 December 2018

STORAGE CLASSES


STORAGE CLASSES 
Every variable In C language has a powerful characteristic called Storage Class.  There are 3 basic places in a c-program where variables will be declared:
  1. Inside the function
  2. in the definition of function
  3. Outside of all functions.
These variables are called as local variables, formal variables and global variables.

Local Variables:
A variable declared inside a function is called as local variables. A local variable will be visible to the function in which it is declared.

Global Variables:
A variable declared in the global variable section are called as global variables. A global variable can be used anywhere in the program. The proper place of declaration of global variable is at the beginning of the main program.

#include<stdio.h>
#include<conio.h>
int a=10;               //      global variable can be accessed from
anywhere in the program

void main( )
{
                   clrscr();
{
                             int x=20;                //local variable can be accessed
with in the function or within the block.
                             printf(“%d%d”,a,x);
                   }
printf(“\n%d”,a);
printf(“\n%d,%d”,a,x);     //error ‘x’ not accessible
}
Output:
10 20
10

A variable defined in a block can be accessed within the block and all its inner blocks. It is called as scope of variable.

While defining a variable we mentioned only its data type. To fully define a variable we need to mention not only its data type, but also its storage class. If we don’t specify it the compiler will assume a default storage class depending on the context where variable is defined.

In ‘C there are 4 types of storage classes.
  • auto
  • static
  • register
  • extern

The storage class tells us 4 things:
  • Default initial value: what will be the default initial value of the variable as soon as it is defined
  • Location: where the variable would be stored
  • Scope: where the variable can be accessed.
  • Life: How long the memory remains reserved for the variable.

By default the storage class for any variable is auto


Auto
Static
Register
Extern
Default initial value
Garbage
0
Garbage
0
Location
RAM
RAM
CPU Registers
RAM
Scope
Local to the block where the variable is defined
Local to the block where the variable is defined
Local to the block where the variable is defined
Entire Program
Life
As long as the control is within the block where  the variable is defined
As long as the program is under execution
As long as the control is within the block where  the variable is defined
As long as the program is under execution

/* Auto Storage Class */

#include<stdio.h>
#include<conio.h>
void fun()
{
          int a=10;
                   printf("%d\n",a);
          a++;
}
void main()
{
          fun();
          fun();
          fun();
}
Output:
10
10
10
Here the storage class is auto be default. Auto variable life is as long as the control is within the block where the variable is defined. Whenever the statement, int a=10; gets executed, memory gets allocated to ‘a’ and the location is initialized with 10. Whenever the control comes out of the fun( ) function block, the variable ‘a’ dies, i.e., memory allocated gets released and hence the output.

/* Static Storage Class */

#include<stdio.h>
#include<conio.h>
void fun()
{
          static int a=10;
                   printf("%d\n",a);
          a++;
}
void main()
{
          fun();
          fun();
          fun();
}
Output:
10
11
12

Static variable life is as long as the program is under the execution. These variables gets memory allocated as soon as the program gets loaded into memory and memory gets deallocated just before the termination of the program from the memory.  
          Static int a=10;
gets executed only once and when the control enters the fun () for the next time the line gets ignored so it has the value what is has the last time. Its value persists between different function calls, and hence the output. These are local to the block.


/* register storage class */

These variables are accessed much quickly than other variables as they reside in the registers of the microprocessor. Because of the limited size and number of registers available, few variables can actually be put in registers. If the compiler does not allocate a machine register for a variable, then the variable is treated as auto. We cannot use register for all types of variables (for ex., long, float, double) because the CPU registers are usually 16-bit registers. As these are declared in registers these cannot be referred by the pointer variable. The variables of most frequent use should only be of register type.

#include<stdio.h>
#include<conio.h>
void main()
{
          register int i;
          for (i=1;i<=100;i++)
                   printf("Hai");
}

Here the i gets accessed around 100 times so if it is made to reside in cpu registers program execution will be fast.
Output:
HaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHaiHai

/* extern storage class */

When there is a need for a variable to be accessed in all the functions in a program then the external variables serve the purpose.
#include<stdio.h>
#include<conio.h>
int a;  /* by default a is 0 */
void main()
{
          a=a+1;
          clrscr();
          printf("%d\t",a);
          fun1();
          fun2();
          fun3();
          fun4();
          getch();
}
fun1()
{
          a=a+10;
          printf("%d\t",a);
}
fun2()
{
          a=a+5;
          printf("%d\t",a);
}
fun3()
{
          a=a-2;
          printf("%d\t",a);
}
fun4()
{
          a=a+8;
          printf("%d\t",a);
}

Output:
1       11      16      14      22