Storage classes in C Programming

  • A storage class defines the scope (visibility) and the lifetime of a variable within a program.
  • The type that is modified will be preceded.
There are four different types of storage classes:

1. Auto storage class

This is the default storage class for all the local variables.

Syntax:
auto int x;

The auto storage class can be used only within the functions i.e. local variables.

2. Register storage class

  • It is used for defining the local variables which are stored in the CPU register instead of RAM.
  • The variable has a maximum size which is equal to the register size.
Syntax:
register int x;

  • The register storage class should be used for the variables which require quick access.
  • If 'register' is defined, it does not mean that the variable should be stored in the register. It means that it can be stored in the register depending on the hardware and implementing issues.

3. Static storage class

  • It is the default storage class for the global variables.
  • The memory for the static variables is allocated when the program begins running and gets free when the program terminates.
Syntax:
static int x = 10;

  • It has an existence throughout the program and instead of getting created and destroyed each time, it comes in and out of the scope.

4. Extern storage class

  • This storage class is used for referencing of the global variables which is visible to all the program files.
  • If there are multiple files in the same program and you want to use a particular function or variable in that file apart from the ones that are declared then you can use the extern keyword.
Syntax:
extern int x;

  • They can be declared anywhere outside the function. But, in general they are defined and declared at the beginning of the source code.
  • The external variables have global scope.