Thursday, July 14, 2011

STORAGE CLASSES : Programming

STORAGE CLASSES

Introduction

The storage class determines the part of memory where storage is allocated for an object (particularly variables and functions) and how long the storage allocation continues to exist.
A scope specifies the part of the program which a variable name is visible, that is the accessibility of the variable by its name.  In C program, there are four storage classes: automatic, register, external, and static.
Moreover, a variable's storage class tells us:
  • Where the variable would be stored
  • What will be the initial value of the variable, if initial value is not specifically assigned(i.e. the default initial value)
  • What is the scope of the variable; i.e. in which functions the value of the variable would be available.
  • What is the life of the variable; i.e. how long would the variable exist.

Automatic Storage Class

The features of a variable defined to have an automatic storage class are as under
Storage
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 the variable is defined
  • The scope of automatic variables is local to the block in which they are declared, including any blocks nested within that block. For these reasons, they are also called local variables.
  • No block outside the defining block may have direct access to automatic variables (by variable name) but, they may be accessed indirectly by other blocks and/or functions using pointers.
  • Automatic variables may be specified upon declaration to be of storage class auto. However, it is not required to use the keyword auto because by default, storage class within a block is auto.
  • Automatic variables declared with initializers are initialized every time the block in which they are declared is entered or accessed.
  • auto is the default storage class for local variables.

Example
void main()    
{
    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

The features of a variable defined to have an automatic storage class are as under
Storage
CPU Register
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 the variable is defined
  • Registers are memory located within the CPU itself where data can be stored and accessed quickly. Normally, the compiler determines what data is to be stored in the registers of the CPU at what times.
  • 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 can't have the unary '&' operator applied to it (as it does not have a memory location).
Example:
      void main()
      {
        register int  Miles;
      }

Static Storage Class

The features of a variable defined to have an automatic storage class are as under
Storage
Memory
Default initial value
Zero
Scope
Local to the block in which the variable is defined
Life
Value of the variable persists between different function calls
  • Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function.
Comparing two storage class we get to know how static storage class variable works and auto storage class works.
Auto Storage Class
Static Storage Class
void Increment()
{
    auto int i=1;
    printf("%d\n",i);
    i=i+1;
}
void main()
{
    Increment();
    Increment();
    Increment();
}
void Increment()
{
    static int i=1;
    printf("%d\n",i);
    i=i+1;
}
void main()
{
    Increment();
    Increment();
    Increment();
}
Output:
1
1
1
Output:
1
2
3
Each time the Increment() function is called i variable is taken as new and each time i is initialized with 1 so when i is printed each time it displays 1
When first time Increment() function is called i variable is initialized to 1 and next two time the value will be incremented by one. That is when Increment() function is called second time i variable in not initialized again but will directly goto printf() and displays the value of i i.e. 2 and so on it displays 3

External Storage Class

The features of a variable defined to have an automatic storage class are as under
Storage
Memory
Default initial value
Zero
Scope
Globle
Life
As long as the program's execution doesn't come to an end
  • All variables we have seen so far have had limited scope (the block in which they are declared) and limited lifetimes (as for automatic variables).
  • However, in some applications it may be useful to have data which is accessible from within any block and/or which remains in existence for the entire execution of the program. Such variables are called global variables, and the C language provides storage classes which can meet these requirements; namely, the external (extern) and static (static) classes.
  • Declaration for external variable is as follows:
  • extern int var;
  • External variables may be declared outside any function block in a source code file the same way any other variable is declared; by specifying its type and name (extern keyword may be omitted).
Example:
#include<stdio.h>
extern int i;    
void Increment()
{    
      i=i+1;
      printf("%d\n",i);
}
void Decrement()
{    
      i=i-1;
      printf("%d\n",i);
}
void main()
{    
      printf("initial value=%d\n",i);
      Increment();
      Increment();
      Decrement();
      Decrement();
}

          Output:

                    initial value = 0
                    1
                    2
                    1
                    0


Book : Let us C

0 comments:

Post a Comment