Storage Classes
Automatic
Default storage class for all local variables that decalred inside a function or block.
Register
Aim to store local variables in the register instead of RAM. This is faster. These variables can keep the adresses of other variables, but cannot have the &
operator applied to them.
Extern Variable
extern
is used to declare a variable that should be defined in other files. Basically, the extern keyword extends the visibility of the C variables and C functions.
The scope of an external variable lasts from the point at which it is declared in a source file to the end of that file.
Static Variable
A static int variable remains in memory while the program is running. It can be both internal or external. E.g. the below count
is static, remains in the memory but only available within the function.
// C Program to illustrate the static variable lifetime
#include <stdio.h>
// function with static variable
int fun()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}