In C variables are declared by the type of data they can hold. During the execution of the program, these variables may be stored in the registers of the CPU or in the primary memory of the computer. To indicate where the variables would be stored, how long they would exist, what would be their region of existence, and what would be the default values, C provides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable. These four storage class specifiers are
The storage class specifier precedes the declaration statement for a variable.
auto int x;
extern int y;
register int z;
static int s;
We will discuss these storage class one by one
1.Automatic storage class
By default, all variables declared within the body of any function are automatic. The keyword auto is used in the declaration of a variable to explicitly specify its storage class. Even if the variable declaration statement in the function body does not include the keyword auto, such declared variables are implicitly specified as belonging to the automatic storage class.
These variables are created automatically, when the function in which it is declared is evoked. These variables are created in primary memory and behaves as local variables of the function. Their region of use is limited within the function body and by default they are not initialized.
The following program explains the use of automatic and global variables and their scope.
auto int i=20;
printf(“The value of i in function f1 is =%d”,i);
printf(“The value of i in function f2 is=%d”,i);
f1(); /* prints 20 ..uses the local variable*/
f2(); /prints 10 ..uses the global variable*/
2.External Storage class
A program in C, particularly when it is large, can be broken down into smaller programs. After compiling these, each program file can be joined together to form the large program. These small program modules that combine together may need some variables that are used by all of them. This provision can be made by specifying these variables as external storage class variable. These variables are global to all the small program modules that are formed as separate files.
The keyword for declaring such global variable is extern. Such global variables are declared like any other variable in one of the program modules while the declaration of these variables are preceded with the keyword extern in all other combining program modules. The program modules may also be a function or a block. These variables remain in existence as long as the program in execution. These variables are stored in primary memory and their default value is zero.
/**** program file prgm1.c ****/
show(); /* show() function in another program file */
printf(“The value of i in prgm1.c=%d”,i);
/***** program file prgm2.c *****/
extern int i;
printf(“The value of i in progm2.c=%d”,i);
The value of i in progm2.c=20
The value of i in progm1.c=20
3.The Register Storage Class
The variables stored in register of the CPU are accessed in much lesser time than those stored in the primary memory. To allow the faster access time for variable, the register storage class specifier is used. In most C compilers register specifier can only be applied to int and char.ANSI C has broadened its scope.The default value of these variable is garbage and their use is restricted within the region of a function or a block where it has been declared.
int power(int,register int);
printf(“The value of %d power %d is=%d”,x,k,p);
int power(int x,register int k)
register int temp;
for(;k;k- -) temp=temp*x;
The value of 4 power 3 is 64
4.The Static Storage Class
The use of static storage class specifier makes a variable a permanent variable within a specified region. Two kinds of variables are allowed to be specified as static variables: local variables and global variables. The local variables are also referred to as internal static variables while the global variables are also known as external static variables. The default value of the static variable is zero.
A static local variable is allotted a permanent storage location in the primary memory. This variable is usable within functions or blocks where it is declared and preserves its previous value held by it between function calls or between block re-entries. However once a function is ivoked, the static local variable retains the value in it and exists as long as the main program in execution.
The external static variables in a program file are declared like global variables with the keyword static preceding its declaration statement. These variables can be accessed by all functions except those functions defined earlier in the same file or not accessible to functions defined in other files although these may use the extern keyword. These variables exist throughout the period of the main program in execution.
static int i;
printf(“The value of static variable i=%d”,i);
printf(“First call of show function\n”);
printf(“Second call of show function\n”);
printf(“Third call of show function\n”);
First call of show function
The value of static variable i=0
Second call of show function
The value of static variable i=1
Third call of show function
The value of static variable i=2