Storage Class
Storage Class
A storage class represents the visibility and a location of a variable. It tells from what
part of code we can access a variable. A storage class is used to describe the following
things:
NOTE: A variable is not only associated with a data type, its value but also a storage
class.
There are total four types of standard storage classes. The table below represents the
storage classes in 'C'.
extern Outside all Memory Zero Entire the file and other program
functions files where the variable runtime
is declared as extern
The variables defined using auto storage class are called as local variables. Auto
stands for automatic storage class. A variable is in auto storage class by default if it is
not explicitly specified.
The scope of an auto variable is limited with the particular block only. Once the control
goes out of the block, the access is destroyed. This means only the block in which the
auto variable is declared can access it.
A keyword auto is used to define an auto storage class. By default, an auto variable
contains a garbage value.
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with in the same storage class. 'auto' can
only be used within functions, i.e., local variables.
The register storage class is used to define local variables that should be stored in a
register instead of RAM. This means that the variable has a maximum size equal to the
register size (usually one word) and can't have the unary '&' operator applied to it (as it
does not have a memory location).
{
register int miles;
}
The register should only be used for variables that require quick access such as
counters. It should also be noted that defining 'register' does not mean that the variable
will be stored in a register. It means that it MIGHT be stored in a register depending on
hardware and implementation restrictions.
Extern stands for external storage class. Extern storage class is used when we have
global functions or variables which are shared between two or more files.
The variables defined using an extern keyword are called as global variables. These
variables are accessible throughout the program. Notice that the extern variable cannot
be initialized it has already been defined in the original file
extern i;
main() {
printf("value of the external integer is = %d\n", i);
return 0;}
i=48;
Result:
The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope. Therefore, making local variables static allows them
to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it
causes that variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a global variable, it causes only one copy of
that member to be shared by all the objects of its class.
#include <stdio.h>
/* function declaration */
void func(void);
main() {
while(count--) {
func();
}
return 0;
}
/* function definition */
void func( void ) {