STORAGE CLASSES IN C PROGRAMING PART-I



STORAGE CLASSES IN A SINGLE FILE:
             The period of time during which memory is associated with a variable is called the extent of the variable. It is characterized by storage classes. Every C variable possesses a characteristic called its storage class. The storage classes are necessary so that by using variable with the appropriate lifetime and visibility, one can write programs using memory more efficiently. Correct use of storage class is especially important in running long programs.

There are four storage class specifies. These are:

  • auto 
  • static 
  • register 
  • extern





 1. auto:
            By default, parameters used in a function as well as variables declared inside a function belong to the automatic storage class. These variables have local visibility, or scope. That is, if you declare a variable called abc in main() and declare another variable with the same name in a function called xyz(), you have created two independent variables, each known only in the function in which it is defined. Anything you do to the variable abc in main(), and vice versa. Also, each variable is allocated when its function begins execution, and each fades away from existence when that function where it is defined terminates.
            Suppose, you define a variable called xyz at the beginning of main(). Now suppose you start a new block with main() and define a new variable, also called xyz, in that block. The program interprets the name xyz to mean the local block variable while the program executes statements within the block. We say the new definition is in scope, and the old definition temporally is out of scope. When the program leaves the block, the original definition of xyz comes back into scope.
For example, the storage  class of the variable num in the example below is automatic
main()
{  int num;
-------------
-------------   }
           Following program shows the working of auto variable
#include <stdio.h>
void  increment ();
main () {
int  i;
for(I = 0; I < 3; i++)
increment();  }
void  Increment ()
{    auto int x=0;
x= x + 1;
printf("x = %d \n", x);  }
Output:

             The program above consist of two functions main() and increment(). The function increment() gets called from main() thrice. Each time increment() function is called, the auto variable x is initialized to zero, and x = x + 1; adds 1 value to x. When the function terminates, its value of 1 is lost, and x is initialized to 0 every time.

2. static:
          A static variable is known only to the function in which it is defined. However, unlike automatic variables, it does not disappear when the function terminates. Instead, it keeps its place and value in memory.
            A variable can be declared static using the keyword static.
For example:
static  int  a;
static  float  b;
          Following program shows the properties of a static variable
#include<stdio.h>
void increment();
main()  {
int i;
for(i = 1; i <= 3; i++)
increment();  }
void increment()
{  static int x = 0;
x = x + 1;
printf("x = %d \n", x);   }
Output:

               In the above program static variable is initialized only once, When the program is compiled. It is never initialized again. During the first call to increment, x is incremented to 1, since x is static, this value persists and the next call adds another 1 i.e. x = x + 1, the current value of i (i.e. 2) is printed and again    x = x + 1, adds another 1, and the value 3 is printed.






3. register:
             The register keyword may be used only for variables declared within function. It makes the variables automatic but also passes a hint to the compiler to store the variable in a register whenever possible. You should use the register keyword for automatic variables that are accessed frequently. Compilers support this feature at various levels. Some do not support this feature at various levels. Some do not support it at all, while others support as many as 20 concurrent register assignment.
             A values stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program it is better to declare its storage class as register. A good example of frequently used variables is loop counters. This is done as follows:
register  int  xyz;
            We cannot use register storage class for all types of variables. Most compilers allow anly int or char variables to be placed in the register.

4. extern:
            The extern specifier may be used for declaring variables within as well as outside a function (except for function arguments). When a program spans across different files, they can share information using global variables. Global variables must be defined only once in any of the program module and they can be accessed by all other. So, use extern storage class for those variables that are being used by almost all the functions in the program.
             In a program where more than two files are in use (i.e. multiple-file), you should define a variable in one and only one file and all other files using that variable have to declare it with the extern keyword. This is the situation where you are designing a system, and more than one programmer is working on the project.
            If you make an external variable static, you need not worry about its name conflicting with external variables found in other files.
FILE1.c :-
#include<stdio.h>
int a = 5;
void func()
{  printf (“%d”,a);
}
FILE2.c :-
#include <stdio.h>
#include <FILE1.c>
main()
{  extern  int a;
int i;
for( i = 0; i < 4; i++)
{ func();
printf("\n");  } }
Output:
STORAGE CLASSES IN C PROGRAMING PART-I STORAGE CLASSES IN C PROGRAMING PART-I Reviewed by vijay pratap singh on July 23, 2017 Rating: 5

No comments:

Powered by Blogger.