C Storage Classes

In this tutorial you will learn about the C Storage Classes and its application with practical example.

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

Functions of storage class

  • It tells the location of the C Variable.
  • Sets the initial or default value of the C Variable.
  • It defines the scope of the C Variable.
  • It defines the lifetime of the C Variable.

Types of C Storage Classes

C Programming Language Supports the following four types 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 a code block and frees up automatically on exiting from the code block. The scope of automatic variables is local to the block where they are declared and is not accessible directly in the other block.

Syntax:

Example :

register – Storage Class

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

Syntax:

Example:

static – Storage Class

It is the 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“, which means variables are 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 class.

Syntax:

Example:

In this tutorial we have learn about the C Storage Classes and its application with practical example. I hope you will like this tutorial.