× Home Introduction Structure of program Variables and keywords Constants Data Types Operators Statements Functions Storage Classes Array Structure Pointer Union Strings Header Files
☰ Topics

Storage Class

'storage' refers to the scope of variable and memory allocated by compiler to store that variable. Scope of a variable is the boundry within which a variable can be used. Storage class defines the scope and lifetime of a variable.

From the point view of C compiler, a variable name identifies physical location from a computer where variable is stored. There are two memory locations in a computer system where variables are stored as: Memory and CPU registers.

Functions of storage class

To determine the location of a varaiable where it is stored ?

Set initial value of a variable or if not specified then setting it to default value.

Defining scope of a variable.

To determine the life of a variable.

Types of Storage Classes:

Storage classes are categorised in 4(four) types as,

Automatic Storage Class

Syntax:

  

auto [data_type][variable_name];

Examples:

  

auto int a; #include< studio.h > #include< conio.h > void main() { auto int i = 10; clrscr(); { auto int i = 20; printf("\n\t %d", i); } printf("\n\n\t %d", i); getch(); } Output: 20 10

Syntax:

  

register [data_type][variable_name];

Examples:

auto int a;

When the calculations are done in CPU, then the value of variables are transfered from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes.

Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again.

It is not applicable for arrays, structures or pointers.

It cannot used with static or external storage class.

Unary and address of (&) cannot be used with these variables as explicity or implicitly.

  

#include< studio.h > #include< conio.h > void main() { auto int i = 10; clrscr(); { register int i = 20; printf("\n\t %d", i); } printf("\n\n\t %d", i); getch(); } Output: 20 10

Static Storage Class

Syntax:

  

static [data_type][variable_name];

Examples:

static int a;

There are two types of static variables as:

a) Local Static Variable

b) Global Static varible

Static Storage class can be used only if we want the value of a variable to persist between different calls.

  

#include< studio.h > #include< conio.h > void main() { int i; void incre(void); clrscr(); for(i=0; i<3; i++) incre(); getch(); } void incre(void) { int avar = 1; static int svar = 1; avar++; swar++; printf("\n\n Automatic variable value: %d", avar); printf("\t Static Variable value: %d", svar); } Output: Automatic variable value: 2Static Variable value: 2 Automatic variable value: 2Static Variable value: 3 Automatic variable value: 2Static Variable value: 4

External Storage Class

Syntax:

  

extern [data_type][variable_name];

Examples:

extern int a;

The variable access time is very fast as compared to other storage classes. But few registers are available for user programs.

The variable of this class can be referred to as 'global or external variables.' They are declared outside the functions and can be invoked at anywhere in a program

  

#include< studio.h > #include< conio.h > void main() { int i=20; void show(void); clrscr(); printf("\n\t%d",i); show(); getch(); } void show(void) { printf("\n\n\t %d", i); } Output: 20 10

Next Topic ➤