Storage Classes are associated with variables for describing the features of
any variable or function in the C program. These storage classes deal with
features such as scope, lifetime, and visibility that help programmers to
define a particular variable during the program’s runtime. The data type
precedes these storage classes that they had to modify.
There are four storage classes types in C:
Auto Storage Class
Register Storage Class
Static storage class
Extern Storage class
Auto comes by default with all local variables as its storage class. The
keyword auto is used to define this storage class explicitly
Int roll; // contains auto by default
Is the same as:
Auto int roll; // in addition, we can use auto keyword
The above example has a variable name roll with auto as a storage class.
This storage class can only be implemented with the local variables.
This storage class is implemented for classifying local variables whose value
needs to be saved in a register in place of RAM (Random Access Memory).
This is implemented when you want your variable the maximum size
equivalent to the size of the register. It uses the keyword register.
Register int counter;
Register variables are used when implementing looping in counter variables
to make program execution fast. Register variables work faster than
variables stored in RAM (primary memory).
For(register int counter=0; counter<=9; counter++)
// loop body
This storage class uses static variables that are used popularly for writing
programs in C language. Static variables preserve the value of a variable
even when the scope limit exceeds. Static storage class has its scope local to
the function in which it is defined. On the other hand, global static variables
can be accessed in any part of your program. The default value assigned is
‘0’ by the C compiler. The static keyword used to define this storage class.
Static int var = 6;
The extern storage class is used to feature a variable to be used from within
different blocks of the same program. Mainly, a value is set to that variable
in a different block or function and can be overwritten or altered from within
another block. Hence it can be said that an extern variable is a global
variable that is assigned with a value that can be accessed and used within
the entire program. Moreover, a global variable can be explicitly made an
extern variable by implementing the keyword extern preceded the variable
name.
Here are some examples of extern:
#include <stdio.h>
Int val;
Extern void funcExtern();
Main()
Val = 10;
funcExtern();
Another example:
#include <stdio.h>
Extern int val; // now the variable val can be accessed and used from
anywhere
// within the program
Void funcExtern()
Printf(“Value is: %d\n”, val);