Storage class in C

Today we are going to discuss about Storage classes in C...

So firstly we will discuss about What is Storage Class?

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’. In other words, not only do all variables have a data type, they also have a ‘storage class’.



If we don’t specify the storage class of a variable in its declaration, the compiler will assume a storage class depending on the context in which the variable is used. Thus, variables have certain default storage classes.
From C compiler’s point of view, a variable name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept - Memory and CPU registers. It is the variable’s storage class that determines in which of these two locations the value is stored.

Moreover, a variable’s storage class tells us:
(a) Where the variable would be stored.
(b) What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value).
(c) What is the scope of the variable; i.e. in which functions the value of the variable would be available.
(d) What is the life of the variable; i.e. how long would the variable exist.

There are four storage classes in C:
(a) Automatic storage class
(b) Register storage class
(c) Static storage class
(d) External storage class

AUTOMATIC STORAGE CLASS
The features of a variable defined to have an automatic storage class are as under:
Storage  −  Memory.
Default initial value  −  An unpredictable value, which is often called a 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 keyword for this storage class is auto.

main( )
{
  auto int i, j ;
  printf ( "\n%d %d", i, j ) ;
}

The output of the above program could be...
1211 221

REGISTER STORAGE CLASS
The features of a variable defined to be of register storage class are as under:
Storage − CPU registers.
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.

A value 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. We can name their storage class as register.

The keyword for this storage class is register.

main( )
{
 register int i ;
 for ( i = 1 ; i <= 10 ; i++ )
 printf ( "\n%d", i ) ;
}

Not every type of variable can be stored in a CPU register.
For example, if the microprocessor has 16-bit registers then they cannot hold a float value or a double value, which require 4 and 8 bytes respectively. However, if you use the register storage class for a float or a double variable you won’t get any error messages. All that would happen is the compiler would treat the variables to be of auto storage class.

STATIC STORAGE CLASS
The features of a variable defined to have a static 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.

The keyword for this storage class is static.

Compare the two programs and their output given below to understand the difference between the automatic and static storage classes.

Program for difference between auto and static storage class


The only difference in the two programs is that one uses an auto storage class for variable i, whereas the other uses static storage class.
Like auto variables, static variables are also local to the block in which they are declared. The difference between them is that static variables don’t disappear when the function is no longer active.
Their values persist. If the control comes back to the same function again the static variables have the same values they had last time around.

But here is a word of advice - avoid using static variables unless you really need them. Because their values are kept in memory when the variables are not active, which means they take up space in memory that could otherwise be used by other variables.

EXTERNAL STORAGE CLASS
The features of a variable whose storage class has been defined as external are as follows:
Storage − Memory.
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to an end.

The keyword for this storage class is extern.

External variables differ from those we have already discussed in that their scope is global, not local. External variables are declared outside all functions, yet are available to all functions that care to use them. Here is an example to illustrate this fact.

int i ;
main( )
{
  printf ( "\ni = %d", i ) ;
  increment( ) ;
  increment( ) ;
  decrement( ) ;
  decrement( ) ;
}
increment( )
{
  i = i + 1 ;
  printf ( "\non incrementing i = %d", i ) ;
}
decrement( )
{
  i = i - 1 ;
  printf ( "\non decrementing i = %d", i ) ;
}
The output would be:
i = 0
on incrementing i = 1
on incrementing i = 2
on decrementing i = 1
on decrementing i = 0

One last thing—a static variable can also be declared outside all the functions. For all practical purposes it will be treated as an extern variable. However, the scope of this variable is limited to the same file in which it is declared.

WHICH TO USE WHEN?
We can make a few ground rules for usage of different storage classes in different programming situations with a view to:
(a) economise the memory space consumed by the variables
(b) improve the speed of execution of the program

Here are some rules for determining which storage class is to be used when and where-

− Use static storage class only if you want the value of a variable to persist between different function calls.

− Use register storage class for only those variables that are being used very often in a program. Reason is, there are very few CPU registers at our disposal and many of them might be busy doing something else. Make careful utilization of the scarce resources. A typical application of register storage class is loop counters, which get used a number of times in a program.

− Use extern storage class for only those variables that are being used by almost all the functions in the program. This would avoid unnecessary passing of these variables as arguments when making a function call. Declaring all the variables as extern would amount to a lot of wastage of memory space
because these variables would remain active throughout the life of the program.

− If you don’t have any of the express needs mentioned above, then use the auto storage class. In fact most of the times we end up using the auto variables, because often it so happens that once we have used the variables in a function we don’t mind loosing them.

Programming for storage classes in C:-

#include <stdio.h> 
int x; 
void Auto() 

printf("\nAuto class\n\n"); 
auto int a = 32; 
printf("Value of the variable 'a'"
" declared as auto: %d\n", 
a); 

printf("--------------------------------"); 

void Register() 

printf("\n Register class\n\n"); 
register char b = 'G'; 

// printing the register variable 'b' 
printf("Value of the variable 'b'"
" declared as register: %d\n", 
b); 

printf("--------------------------------"); 

void Extern() 

printf("\nExtern class\n\n"); 

extern int x; 
printf("Value of the variable 'x'"
" declared as extern: %d\n", 
x); 
x = 2; 
printf("Modified value of the variable 'x'"
" declared as extern: %d\n", 
x); 

printf("--------------------------------"); 

void Static() 
int i = 0; 

printf("\nStatic class\n\n"); 
printf("Declaring 'y' as static inside the loop.\n"
"But this declaration will occur only"
" once as 'y' is static.\n"
"If not, then every time the value of 'y' "
"will be the declared value 5"
" as in the case of variable 'p'\n"); 

printf("\nLoop started:\n"); 

for (i = 1; i < 5; i++) { 
static int y = 5; 
int p = 10; 
y++; 
p++; 
printf("\nThe value of 'y', "
"declared as static, in %d "
"iteration is %d\n", 
i, y); 
printf("The value of non-static variable 'p', "
"in %d iteration is %d\n", 
i, p); 

printf("\nLoop ended:\n"); 

printf("--------------------------------"); 

int main() 

printf("program to demonstrate"
" Storage Classes in C\n\n"); 
Auto(); 
Register(); 
Extern();
Static();  
printf("\n\nStorage Classes"); 

return 0; 

written by- 
Ritik and Rohan Tyagi
                                                                                                     


Comments

Post a Comment