Sunday, January 8, 2012

Storage Class Specifiers


 Storage Class Specifiers
The storage class specifier refers to how widely it is known among a set of functions in a program. In other words, how the memory reference is carried out for a variable. Every identifier in C++ not only has a type such a integer, double and so on but also a storage class that provides information about its visibility, lifetime and location. For example, if the variable belongs to an automatic type, that means whenever the variable is used in a main program or a function, the computer will automatically reserve a memory space for it. Normally, in C++, a variable can be declared can be belong to any of the following groups:
(1)      Automatic variable
(2)     Register variable
(3)     Static variable
(4)     External variable

 Automatic Variable
Internal or local variables are the variables which are declared inside a function. Instead, they are more often referred to as automatically due the fact that their memory space is automatically allocated as the function is entered and released as soon as it leaves. In other words, automatic variables are given only temporary memory space. They have no meaning outside the function in which they are declared. The portion of the program where a variable can be used is called the scope of that variable. Automatic variables can be declared not only at the beginning of a function but also at the beginning of a compound statement (also called a block).
    Local variables are given the storage class auto by default. One can use the keyword auto to make the storage class explicit but no one does, since a declaration
    #include <iostream.h>
    void main (void)
    {
         auto int a, b, c ;      



           
      }
     is exactly equivalent to
      #include <iostream.h>
      void main ( void)
      {
          int a, b, c ;


            
        }
  The general syntax of an automatic variable is,
                storage_class  data_type  variable_1, variable_2...variable_n;
  Here the storage class is an automatic, so it can be written as
      auto x, y, z ;
     auto float a1,a2 ;
     auto char name1 ;
  But the above declaration can also be written as
       int x, y, z ;
      float a1, a2 ;
      char name1 ;
  However, both the declarations are same. The keyword auto used only if one desires to declare a variable explicitly as an automatic variable.

PROGRAM
In the following program the identifier ‘i’ used for three distinct variables.
     / / automatic variable declaration
    #include <iostream <iostream.h>
    void main (void)
    {
        void funct (int i) ; / / function declaration
        int i = 1; / / this I belongs to function main ( )
        funct (2) ;
        cout << “i = “ << i << “ (in function main) “ << endl ;
    }
    void funct (int i) ; / / outermost i in main
    {
         i = i*10;
         {         / / here is a compound statement with another i
                   int i ;
                   i = 3 ;
                   cout << “ i = “ << i << “ (in inner most block)” ;
                   cout << endl ;
          }
          cout << “i = “ << i << “ (parameter of function f)” ;
          cout << endl ;
     }
Output of the above program
    i = 3 (in inner most block)
   i = 20 (parameter of function f)
   i = 1 (in function main)
   The automatic variables have two distinct advantages. First, the memory space is used economically since it is used only as long as it is needed. Secondly, their local scope prevent from affecting other functions due to inadvertent usage. Hence, variables in other functions need not necessarily be given different names.

Register Variable
Automatic variables are stored in the memory. As accessing a memory location takes time (much more time then accessing one of machine’s registers), one can make the computer to keep only a limited number of variables in their registers for fast processing. Whenever some variables are to be read or repeatedly used, they can be assigned as register variables.
    The general syntax of a register variables is,
               register datatype variable_1,variable_2......variable_n;
    The keyword register is used to declare that the storage class of the variable is a register type.
For a limited number of variables it is possible to make the computer to keep them permanently in fast registers. Then the keyword register is added in their declaration.
    For example,
         function (register int n)
       {
              register char temp;


                                                       
       }
   If possible, machine registers sometimes called accumulators, can be assigned to the variables n and temp, which would increase the speed. If there are not enough register variables then the request will simply be ignored.
PROGRAM 
A program to display the number and its square from 0 to 10 using register variables.
    / /register variable declaration
   #include <iostream.h>
   void main(void)
   {
       int funct (register int x, register int y) ;
       / / function declaration
       register int x, y, z ;
       x = 0;
       y = 0;
       cout << “ x        y “ << endl;
       cout << “                ” << endl;
       do  {
              z = funct (x,y) ;
              cout << x << ‘\t’ << z << endl;
              ++x;++y;
       }
       while (x <= 10) ;
}
int funct (register int x, register int y)
{
     register int temp;
     temp = x*y;
}
  Unfortunately, there are some restrictions on register variables. As a machine register is usually a single word, many compilers allow only those variables that fit into a word to be placed in registers. This means that int, char, or pointer variables can only be placed in registers. In addition, most compilers have only a few registers available to user programs, usually two or three. Suppose, some of the variables have been declared as register variables and if these the variables is not the correct data type such as char or int and if there are not enough registers available, then the C++ compiler will automatically ignore the register data type and it keeps them in the memory, treating them as automatic variables.

 Static Variable
Static variables are defined within a function and they have the same scope rules of the automatic variables but in the case of static variables, the contents of the variables will be retained throughout the program.
     The general syntax of the static variables is,
                    static    datatype   variable_1, variable_2....variable_n;
     Where static is a keyword used to defined the storage class as a static variable.
     The following declarations are valid
            static int x,y;
          static int x= 100;
          static char a1,ch;

PROGRAM 
A program to display 1 to 10 with addition of 100 using the automatic variable and the static variable.
    / /using the automatic variable
   #include <iostream.h>
    void main (void)
    {
        int funct (int x); / / function declaration
        int i, value;
        for (i = 1, I <= 10; i++) {
              value = funct (i);
              cout << i << ‘\t’ <<value << endl;
        }
     }
     funct (int x)
     {
           int sum = 100;  / /automatic variable
           sum += x;
            return (sum);
      }
Output of the above program
1                    101
2                    102
3                    103
4                    104
5                    105
6                    106
7                    107
8                    108
9                    109
10                  110
    The above program can be modified using sum as a static variable:
    / /using the automatic variable
   #include <iostream.h>
    void main (void)
    {
        int funct (int x); / / function declaration
        int i, value;
        for (i = 1, i<= 10; i++) {
              value = funct (i);
              cout << i << ‘\t’ <<value << endl;
        }
     }
     funct (int x)
     {
          static int sum + 100; / /static variable
          sum += x;
          return (sum);
      }
Output of the above program
1                    101
2                    103
3                    106
4                    110
5                    115
6                    121
7                    128
8                    136
9                    145
10                  155
    Since sum has a permanent memory space it retains the same value in the period of time between leaving function and again entering it later. In contrast to automatic variable, static variables are initialized only once. Hence in the above program, sum has the value 100 only.
  External Variable
Variables which are declared outside the main are called external variables and these variables will have the same data type throughout the program, both in main and in the functions.
  Normally, variables are declared only at the beginning of blocks. But one can also declare variables outside any function, anywhere in the file. Functions can access these variables, which resemble the global variables of Pascal and the common variables of FORTRAN, simply by referring of them by their name. Global variables have the same life and initialization rules as the static variables.
   This implicit initialization is convenient and is taken advantage of in countless programs, but explicitly initialization global variables makes programs more readable.
   The general syntax of the extern variable is,
                   extern  datatype  variable_1, variable_2...variable_n;
Where extern is a keyword used to defined the storage class as external variables.
The following declarations are valid
   extern int x,y;
   extern float p,q,r;
   extern char a1,ch;

PROGRAM 
A program to define a variable as an external data type and display the content of the variable.
    / /using the automatic variable
   #include <iostream.h>
    int x;
    void main (void)
    {
        void funct (void);
        x = 10;
        funct ();
    }
    void funct (void)
    {
        cout << “ content of x = “ << x << endl;
Output of the above program
    content of x = 10
     In the above program, x hase been declared as global variable, in other words, as any external variable. One has to declare the external variables only once but these external variables can be used in both main and functions without declaring the data type again.
PROGRAM
A program to defined the variables as an external data type.
    / /using the automatic variable
   #include <iostream.h>
   int n = 4;
   int i;
   void main (void);
   {
        void funct (void)
        for (i = 1; i <= n-1; ++i) {
             cout << “ inside the main \n”;
             cout << “ Komputer \n”;
             }
        funct ();
   }
   void funct (void)
   {
        for (i = 1; i <= n-1; ++i) {
              cout << “ inside the function \n”;
              cout << “ Kompu \n”;
              }
   }
   void funct (void)
   }
       for (i = 1; i <= n-1; ++i) {
            cout << “ inside the function \n”;
            cout << “ Kompu \n”;
            }
   }

No comments:

Post a Comment