Storage Classes in C


Where the variables are declared in a C program, is determined by storage classes. The scope and life time of variables is defined by storage variables. It is very clear from the name storage classes that it specifies how variables are stored in the memory. In other words we can say that every variable has a storage class that determines the life time and scope of the variable. The following are the storage classes that are used in C programming language:

  1. Local Variables
  2. Global variables or external variables
  3. Static variables
  4. Automatic variables
  5. Register variables


Local Variables:

These variables are declared inside any module in a C program. A variable declared inside a function is known as local variable. Local variables are also called automatic variables. The following is the syntax to declare local variables:

data-type identifier;

Here data-type is the data type of the variable and identifier is used to indicate the name of the variable.

Scope of local variables:

The area where a variable can be accessed is known as scope of variable. Local variable can be used only in the function in which it is declared. If a statement accesses a local variable that is not in scope, the compiler generates a syntax error.

Lifetime of local variables:

The time period for which a variable exists in the memory is known as the lifetime of that variable. Different types of variables have different lifetimes.

The lifetime of local variable starts when the control enters the function in which it is declared. Local variable is automatically destroyed when the control exits from the function and its lifetime ends. When the lifetime of a local variable ends, the value stored in this variable also becomes inaccessible.


Global Variables:

A variable declared outside a program module is global variable. All functions in the program can access these variables. Their values are shared among different functions. If one function changes the value of a global variable, this change is also available to other functions.

Scope of global variables:

Global variables can be used by all functions in the program. It means that these variables are globally accessed from any part of the program. Normally they are always declared after the header files and before the main () function.

Lifetime of global variables:

Global variables exist in the memory as long as the program is running. These variables are destroyed from the memory when the program terminates. These variables occupy memory longer than local variables. So, global variables should be used only when these are very necessary.

Consider the following code which inputs a number in global variable. It calls a function that multiplies the global variable by 2. The main function then displays value of global variable:

CODE:

# include <stdio. h>

# include <conio. h>

int g;

void fun ();

void main ()

{

printf (“Enter a number”);

scanf (“%d”, &g);

printf (“Value of g before function call: %d\n”, g);

fun ();

printf (“Value of g after function call: %d”, g);

getch ();

}

void fun ()

{

g = g * 2;

}

OUTPUT:

Enter a number: 5

Value of g before function call: 5

Value of g after function call: 10

In the above example we used functions (declaration, calling and definition of function) which will be discussed in the next topic.


Static Variables:

Static variables are initialized once at the beginning of the program. The value of static variable remains constant. They are sometimes used to count objects referring to object oriented programming. The static variables are initialized only once in a program and their values are not changed and they cannot be destroyed whether they are in scope or out of scope.

The internal static variables or local static variables are those that are declared inside the function and their scope remains inside the function in which they are declared. The scope of external or global static variables remains to the file in which they are declared. The initial value of static variable is zero which is assigned by the compiler by default.

Scope of static variables:

Just like global variables, static variable can also be used by all functions of a program.

Lifetime of static variable:

The lifetime of static variables is equal to the lifetime of the program in which they are used.


Auto variable:

Local variables are also called automatic variables. Automatic variables have the same scope and lifetime as the local variables.

When a variable is declared locally, by default it is declared as an auto variable. We can use the auto keyword before declaring a local variable which indicates that the variable is automatic. Consider the following syntax to declare an automatic variable:

auto data-type identifier;

Here auto is the keyword that indicates that the variable is automatic; the auto keyword is not necessary to declare an automatic variable, a variable is declared automatic by default. Data-type is the type of the variable and identifier indicates the name of the name of the variable. The automatic variables are created when we call a function and are destroyed when the control is returned from the function. An automatic variable is assigned a null value by default by the compiler.


Register variable:

When a variable is created it is automatically stored in RAM. To store a variable in a register, register variable is used. These variables are accessed fast by the compiler. The register variable tells the compiler to store the variable in register instead of storing the variable into memory. Register have a limited storage and only a few variables can be stored in the register. Usually the variables that are used frequently are stored in a register. We cannot get the address of a register variable.

Consider the following syntax to declare a register variable:

register int num;

Scope of register variable:

Register variable is a type of local variable it means it can only be used inside a function where it is declared.

Lifetime of register variable:

Register variable destroys when the control exits from the function where it was declared.


extern keyword:

We use the extern keyword while declaring a variable to tell the compiler that this variable is declared somewhere. When we use the extern keyword the variable is not allocated any storage. Suppose we have declared a global variable in one file then we can use that variable in another file by using the extern keyword. When we do not use the extern keyword while using the global variable declared in another file then an error will be generated that this variable is not found.

Consider the following example in which we have used the extern keyword:

CODE:

# include <stdio. h>

# include <conio. h>

int y;    

void main ()

{

  extern int y;

  y = 10;

  printf (“%d”, y);

}

In the above example extern keyword is declaring that the variable y is defined somewhere else.