In this segment you will be familiarized with the storage class available for C programming langauge along with their syntax.
Storage class:
The storage location and scope or visibility of a variable or a function within a program is defined under storage classes in C.
There are four types of storage classes available in C , which are given as follows:
- Automatic / Auto
- Static
- Register
- Extern
Automatic or Auto storage class :
- Automatic or Auto is a default storage-class for the basic variables and function (where a class does not mentioned before the variable )that we define or declare under a C program.
- Hence the keyword auto is rarely used while writing a C program.
- Auto variables or functions can be only accessed within the block/function in which they have been declared i.e. scope of auto variable is within the block of code.
- They are assigned a garbage value by default whenever they are declared.
- They get destroyed on exit from the block of code.
For example :
int count;
or auto int count;
Both are similar as by default compiler takes the storage-class as auto for all variables and functions declared under a program.
In above example auto is a keyword which is used to indicate static variables.
Static storage class:
- Static type of variable is widely used in C programming language as static have a property to store the value of variable even if it is out its scope.
- Static variable can not be accessed outside the program i.e. scope of auto variable is within the block of code but the maintain the value between two funtion calls.
- Static variables have default value equal to zero and initialized only once in their lifetime.
For example :
static int count;
In above example static is a keyword which is used to indicate static variables.
Register storage class:
- Functionality of register class is same as the auto.
- But the only difference is the values are stored directly into the registers of microprocessors i.e. they can be accessed directly.
- They can be only accessed within the block/function in which they have been declared i.e. scope of auto variable is within the block of code.
- They are assigned a garbage value by default whenever they are declared.
- They get destroyed on exit from the block of code.
For example :
register int count;
In above example register is a keyword which is used to indicate static variables.
Extern storage class :
- Extern stands for external class.
- It means variable is declared externally i.e. not in the program or set of code
For example :
extern int count;
In above example extern is a keyword which is used to indicate static variables.
This is all about the storage class available for C programming tutorial along with their syntax.
If you have in doubt please comment below.