Storage Classes in C

Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of a particular variable during a program’s runtime.

  • auto
  • register
  • static
  • extern

The auto Storage Class

For all local variables, the auto storage class is the default storage class.

The example above differentiates two variables in the same storage class.

Only functions, i.e., local variables, can be used in ‘auto’

The static Storage Class

The static storage class instructs the compiler to retain a local variable in existence throughout the program’s lifespan, instead of generating it and deleting it as it comes in and goes out of control. Hence, the stratification of local variables helps maintain their values between function calls.

Also, the static modifier may be applied to global variables. When this is achieved, it causes the scope of that variable to be limited to the file that declares it to be in.

The register Storage Class

This class of storage declares registry variables that have the same functionality like the auto variables. The only difference is that if a free register is available, the compiler tries to store those variables in the microprocessor’s register. It makes use of registry variables much faster than those stored in the memory during the initialization of the program. If there is no free register available, these are stored only in the memory. Generally, several variables are defined with the register keyword, which helps in increasing the system runtime to be obtained within a program. An important and interesting point to note here is that we cannot use the pointers to retrieve the address of a registry variable.

In C programming language, when static has been used on a global variable, it allows all members of its class to share only one copy of that item.

Output:

Storage Classes in C

The extern Storage Class

The extern storage class is used to provide a comparison of a global variable that is accessible to ALL the files in the program. However, when you use ‘external,’ the variable cannot be initialized; it points the name of the variable to a predefined storage location.

Example:

Output:

Storage Classes in C

Pin It on Pinterest

Share This