Storage Class in C Programming
This article is about what is a storage class and the different storage classes in c.
Storage Class in C programming
The storage class defines the location, visibility and lifetime of variables and functions inside a C program. It gives information about the variable. There are four different storage classes in the C program. They are as follows:
- Auto
- Static
- Register
- Extern
Auto Storage Class
This is the default storage class in C and the variables defined in this class are called local variables. The scope of a variable in this class is limited within a particular block only. The auto variables cannot be accessed outside the specific block. It contains a garbage value.
Example auto int age=21;
Static Storage Class
In static storage class, the variables can be defined in two ways.
- Static local variable is a variable that retains its value between block or function calls and remains visible only to the block or function in which it is defined.
- Static global variables are variables visible only to the file where it is declared.
- The static variables by default hold the value zero.
Example static int year = 1999;
Register storage class
The variables declared in the register storage class are limited only to a particular block just like the auto storage class but the register storage class stores local variables within blocks or functions in CPU registers instead of RAM. This would help in quick access to the variables stored. Variables defined using the register storage class has a lifetime throughout the program. These variables have no default value and are often declared at the top of the program.
Example register int age;
Extern storage class
Global variables are declared using an extern storage class where the variables are shared between two or more files. Extern denotes external storage class.
Example extern i;