C Storage Classes

In C Programming Language a “Storage Class” refers to the scope or visibility and the life time of the C Variable. Scope of the C Variable defines the availability of the variable in block of the C Program, and by life time of variable means how long variable will persist in the program.

Functions of storage class

  • It tell the location of the C Variable.
  • Sets the initial or default value of C Variable.
  • It defines the scope of the C Variable.
  • It defines the life time of C Variable.

Types of Storage Classes

C Programming Language Supports following four type of storage classes:

  • auto
  • register
  • static
  • extern

auto – storage class

It is the default storage class for local variables. Variables with auto storage class are declared at the beginning of a code block, and memory is allocated automatically as the program execution enters to a code block and frees up automatically on exiting from the code block. The scope of automatic variables is local to the block where the declared and are not accessible directly in the other block.

Syntax:

Example :

register – Storage Class

C Variables with register storage class are same as local variables to the code block but the are stored in CPU register instead of computer memory. Hence it enables the quick access of that variable. Maximum size of the variable is equal to register size and dependent upon the register size

Syntax:

Example:

static – Storage Class

It is default storage class for global variables.Static storage class can be used only if we want the value of a variable to persist between different function calls. C Variable declared with static storage class will keep its value retained for different function calls.

Syntax:

Example:

extern – Storage Class

C Variable declared with extern storage class is said to be “Global Variables”, it means variables is accessible throughout the program till the end of program execution. External variables are declared outside the functions and can be invoked from and anywhere in a program.External variables can be accessed very fast as compared to any other storage classes.

Syntax:

Example: