What is Storage Class in C ?
Storage classes use to give the following information about the variable
- Default value
- Storage
- Scope
- Life
Default value
If we don’t assign anything in the variable then depend upon storage class some value automatically assigned (It could be either garbage or zero) in the variable.
#include
main()
{
int x;//garbage value stored
}
Storage
Variable normally get the memory in the RAM but if any variable need to be called frequently to perform logics and instructions in CPU, it may directly store in CPU registers. In this way we essentially save the code run time.
Scope
Scope of the variable is the extent up to which particular variable would be accessible. It could be upto single block ‘{}’ or in entire program, depend on storage class.
Life
Life of the variable is the duration up to which allocation of the memory has been done in side the RAM for particular variable. Life of the variable could end after executing single block ‘{}’ or after finishing entire program. It completely depend on the storage class.
Type of Storage class
- Automatic
- Register
- Static
- External
Storage class | Keyword | Default value | Storage | Scope | Life |
---|---|---|---|---|---|
Automatic | auto | Garbage | RAM | Limited to block in which it is defined | Till the execution of the block |
Register | register | Garbage | cpu register | Limited to block in which it defined | Till the execution block |
Static | static | 0 | RAM | Limited to block in which it defined | Till the end of the program |
External | extern | 0 | RAM | Global | Till the end of the program |