STORAGE CLASSES IN A MULTIPLE SOURCE FILES



STORAGE CLASSES IN A MULTIPLE SOURCE FILES:
         The storage class defines two characteristics of variables. These are:

  • Lifetime 
  • visibility or scope




1. Lifetime:
        The lifetime of  variable is the length of time the program retains a particular value of a variable in the memory of a computer. In terms of their lifetime, variables can be divided into two categories:

  • Automatic variables 
  • Static and the external variables

A.  Lifetime of Automatic Variables:
         Automatic variables are the most commonly used variables. In a program’s source file, automatic variables are written inside braces ({ }) that serve as delimiters for a function. They are created in the memory when the function containing them is called, and destroyed as soon as the function terminates. For example, in the following program segment:
void
func(void)
{ int alpha;
auto int beta  }
         The two variables are created when the function func() is called and destroyed when it is finished and control returns to the calling function. The variables alpha is  automatic by default, while beta has been made automatic explicity, using the auto keyword. The effect is exactly the same, but the keyword auto is sometimes used to avoid confusion.

B.  Lifetime of Static and External Variables
        If you want a variable to retain its value after the function that defines it is terminated, then you have to declare the variable to be of type static.
The following program segment demonstrates this:
func()  {
static int delta;  }
        In the above example, even after the function func() is terminated, delta will retain the value it was given in the function or its modified value during the execution of the function.
       Another way to ensure that a variable retains its value throughout the course of a program it to make it external. This is done by placing the variable out of any function.
      Like static variables, external variables exit for the life of the program. But they differ in their visibility.




2.  Visibility
      The visibility (or scope) of a variable refers to parts of a program that will be able to recognize it. There are more distinctions involved in visibility than there are in its lifetime. A variable may be visible in a block of a program or within a function. It may be visible in a file, a group of file, or in the entire program.

A.  Visibility of Automatic and  Static Variables
      An automatic variable is visible and recognized within the function in which it is defined. Therefore it is called a local variable. For example:
void func()  {
int  eta;   }
main()
   { --------}
     The variable eta is recognized only int function func(). It is not recognized in the function main(). If we use another variable called eta in main(), then these two variables even though they have the same names, they would b recognized differently by C compiler.
      The visibility of automatic and static variables can be restricted even further. They can be defined inside a block. A block is a section of a program set off by braces. For example:
main()  {
int epsilon;  {
int pi;  }  }
        In the above program segment, the variable pi is visible only within the inner set of braces, while epsilon is visible throughout the function main(). The braces used to group statement in the if or while constructs from blocks. Variable define within such blocks will not be visible outside that block.

B.  Visibility of External Variables
         To create a variable that is visible to more than one function, we must make it external. Thus zeta in the following program will be visible to both main() and func() or any other function placed after the definition of zeta in this file.
int zeta;
void func(void) 
{--------}
main()
   {-----------}
         An external variable is therefore, visible only to those function that it in the file. The declaration of variables using the extern keyword follows the same visibility rules as the definition of variables.
STORAGE CLASSES IN A MULTIPLE SOURCE FILES STORAGE CLASSES IN A MULTIPLE SOURCE FILES Reviewed by vijay pratap singh on August 02, 2017 Rating: 5

No comments:

Powered by Blogger.