What are the Storage Classes in C
It defines the scope, visibility and life time of variable. There are 4 types of storage classes in C:
1. auto – It is the default storage class for every local variables. When a function is called they are created and when the function exits they are destroyed automatically.
auto int data; //Both are same
2. register – It defines the local variables which are stored in register rather than RAM. Register variables have faster access than the normal variable.
register int data;
3. extern – It gives the reference of a global variable which is visible to all program files. It keyword is used before a variable to tell the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables.
Suppose there are two files a and b.
File a contains:
int i=10; / /Global variable
File b contains:
extern int i;
int i; //Global variable
extern int i; //Informs compiler that it is defined somewhere else i = 20;
4. static – It tells the compiler to keep a local variable in existence during the life time of the program rather than creating and destroying it every time it comes into and goes out of scope. So making local variables static permits them to keep their values between function calls. They are assigned 0 as default value by the compiler.
static int i = 0; //Static variable
i = i + 1;