/* EXample of storage class*/
int m;
main()
{
int i;
float balance;
....
function1();
}
function1()
{
int i;
float sum;
...
...
}
THe variable m which has been declared before the main is called global variable
It can be used in all functions in program. It need not be declared in other functions.A global
variable is also known as an external variable.
THe variable i, balance and sum are called local variables because they are
declared inside a function.
Storage classes in C allocate the storage area of a variable that will be retained in the memory.
They are stored in the RAM of a system. Apart from the storage space, they determine the scope of a variable.
Variables in the C program are stored in a physical location in the random memory, which is mainly the
device's memory and CPU registers.
Storage classes in C also define the lifetime of the variable and term it as 'local' or 'global'.
Storage classes are also useful to define the scope or visibility, and the initial value of the
variable. There are primarily four storage classes in C, viz. automatic, register, static, and
external. We will discuss each one by one further.
The four storage classes in C are declared in a block or program with the storage class specifiers,
auto, register, extern, static. There is one more storage class specifier, ‘typedef’ used in the
syntactic form, and does not reserve storage. The specifiers instruct the compiler on storing the
variables. The external storage class in C tell the compiler that variable defined is declared
with external linkage.
There is a key difference between defining and declaring a variable. Defining a variable
is about allocating the memory for the variable and declaring it means initializing it with a value.
THe storage class is another qualifier(like long or unsigned) that can be added to a
variable declaration as shown below:
auto int count;
register char ch;
static int x;
extern long total;
Storage class | Meanings |
---|---|
auto | Local variable knows only to the function in which it is declared. |
static |
Local variable which exists and retains its value even the control is transferred to the calling function. |
extern | Global variable known to all functions in the file. |
register | Local variable which is stored in the register. |