Storage classes in C programming


Every variable in C is associated with a storage class in addition to its data type and name.

A variable’s storage class specifier tells us :

  1. The place where the variable may be stored: Memory or CPU registers.
  2. The default initial value of the variable, if the initial value is not specified explicitly in the program.
  3. The scope of a variable species the area of the program where it can be accessed.
  4. The lifetime means how long the variable stays in memory.

There are four basic types of storage classes available in C as follows:

  1. Automatic storage class
  2. External storage class
  3. static storage class
  4. Register storage class
  1. Automatic Storage Class

The keyword of this storage class is auto. It specifies that a variable will exist only within the block in which it is declared. They are declared at the start of the program;s block such as in the curly braces. memory for such variables is allocated automatically when a block is entered and freed automatically upon exit from the block, hence the name automatic. Te scope of automatic variable is local to the block in which it is declared including any blocks nested within that block. No block outside the definning block have direct access to automatic variables. Therefore, all local variables have auto storage class by default. 

Program of auto storage class:

#include<stdio.h>

main()

{

void add7();

add7();

add7();

}

void add7()

{

auto int x=20;

x=x+7;

printf(“x is = %d”,x);

}

output: x=27

x=27

x=27

 

2. EXTERNAL STORAGE CLASS

A variable in the global declaration section has, by default, extern storage class. Scope of the external variable is throughout the program. The features of the variable having extern storage class are as under:

  1. By default, storage location is memory.
  2. By default its initial value is zero.
  3. Lifetime of the variable is global

3. STATIC STORAGE CLASS

The static variable can be applied both to local and global variable. These have the scope local in the block in which it is declared but it has global life time i.e its value persists between different function calls.

The features of the variable having static storage class are as under:

  1. By default, storage location is memory.
  2. By default its initial value is zero.
  3. Lifetime of the variable is global
  4. scope is local

Program of static storage class:

#include<stdio.h>

main()

{

void add7();

static int y;

printf(” y is = %d”,y);

add7();

add7();

}

void add7()

{

auto int x=20;

x=x+7;

printf(“x is = %d”,x);

}

output:

y=0

x=27

x=34

x=41

 

4. REGISTER STORAGE CLASS

Registers are special storage areas within the CPU of computer. Whenever some arithmetic or logical operation is carried out, the data is transferred from computer’s memory to register and the operation is carried out. after the computation, result result is sent back to memory.

syntax : register int x;

The features of the variable having register storage class are as under:

  1. Its storage location is CPU register.
  2. It takes garbage value as initial value, if not initialized.
  3. Lifetime of a variable is from the point of declaration till the end, control remains within the block.
  4. Scope is local.

Leave a comment