Storage class in C
Storage class is basically used to specify the scope and lifetime of a variable. It specifies the place where the variable may be stored i.e. memory or CPU registers. It also specifies the initial value of a variable, if the initial value is not specified in the program. It also specifies the visibility of a variable i.e. which parts of a program can access the variable and how long the variable stays in the memory. There are four types of storage classes in C language.
- Storage class auto (Automatic variables)
- Storage class register (Register variables)
- Storage Class static (Static variables)
- Storage Class extern (External variables)
1. Storage Class auto (Automatic variables)
Automatic variables are declared inside a function in which they are to be used. They are created when the function is called and destroyed automatically when the function is exited.
They are local to the function in which they are declared.
Because of this property, automatic variables are also referred as local or internal variables.
A variable declared inside a function without any storage class specification is by default an automatic variable.
We can also use keyword auto with variable declaration to specify that the variable is an automatic variable.
auto int a;
One important property of an automatic variable is that its value can’t be changed accidently by other function which assures that we can declare and use variable with same name in different functions in the same program without causing any confusion to the compiler.
Automatic variables can also be defined within a set or braces known as blocks. They are meaningful only inside a block where they are defined.
- The same auto variable name can be used in different blocks.
- There is no side effect by changing the values in the blocks.
- The memory is economically used.
- Auto variables have inherent protection because of local scope.
|Program to demonstrate the use of automatic variables|
auto int i=10; /*or int i;*/
2. Storage Class register (Register variables)
Register variables are those variables which are kept in one of the computer’s registers instead of main memory where normal variables are declared.
The memory access of register variables is faster as compared to normal variables which are kept in memory. We should declare lesser number of variables of register type as there is limited number of registers in computer.
We should declare only those variables to be register type which are frequently used (like loop control variables). Because only a few variables can be placed in the registers, we should be very careful in selecting the variables, which we want to keep in register.
We can use the keyword register with the variable declaration to specify that we are declaring register variables.
register int a;
In the above declaration, a is an int type variable which has been defined in the computer register.
- The processing speed of register variable is high.
- Only few register variables can be declared because number of registers is very less.
|Program to demonstrate the use of register variables|
register int i=10;
3. Storage Class static (static variables)
Static variables are special types of variables, which retain their value throughout the program. Static variables are initialized only once when program is compiled. Static variables are not initialized again. We can use the keyword “static” to declare static variables. A static variable can be defined in two ways:
- Internal static variable
- External static variable
i. Internal static variable
Internal static variables are those, which are declared inside a function. The scope of internal static variables lies within the function in which they are declared.
Internal static variables are similar to automatic variables except that they remain in existence throughout the program. Internal static variables can be used to retain values between function calls.
ii. External static variable
External static variables are declared outside all the functions and they are available to all the function within the program.
The main difference between a normal external variable and static external variable is that the normal external variable is available within a file only whereas static external variables can be accessed also by other files.
Advantages of static variable
- We can preserve the value of a variable throughout the program within a function.
- In a recursive function, if we declare a static variable, it will make the program faster and use less memory space.
|Program to demonstrate the use of static storage class|
static int i=10;
In the above program, when the function show () is called for the first time, static variable i is initialized with value 10.
When this function is called again, variable i will not be initialized again with 10 but it will take its previous value and increment it further with one with each call to function show ().
4. Storage Class extern (External variables)
External variables are special types of variables which retain their value throughout the program. They are also known as global variables. External variables are always declared outside a function.
You can use the keyword extern to declare a variable to be external variable. The very main feature of external variable is that we can make any variable global by using the keyword extern irrespective of the position of declaration of a variable in a program.
We can access external variables across multiple files. In such a case, we can declare a variable in one file and call it within another file using the keyword extern with the variable declaration.
- Persistent storage of a variable retains the latest value.
- Value is globally available.
- The storage for an external variable exists even when the variable is not needed.
- The side effect may produce surprising output.
- Modification of the program is difficult.
- Generality of a program is affected
|Program to demonstrate the use of extern storage class|
extern int i;
extern int i;
|In the above program, variable i has been defined after show() function, it can’t be directly within the main() function.|
But it has been made available within main() function by redeclaring the variable i within the main() function by using the keyword extern. The variable i will retain its value throughout the program.
TIPS and tricks
1. There are 3 main uses for the static.
* If you declare within a function, it retains the value between function calls
* If it is declared for a function name, By default function is extern. so it will be visible from other files if the function declaration is static. It is invisible for the outer files.
* Static for global variables: By default we can use the global variables from outside files If it is static global. Its scope is limited within the file.
2. C has three types of storage:
automatic, static and allocated.
Variable having block scope and without static specifier have automatic storage duration.
Variables with block scope, and with static specifier have static scope. Global variables with or without the the static specifier also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class.
3. A static function is a function whose scope is limited to the current source file.
Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.
Test Your Knowledge
Write the output of the following programs:
static int x=5;
void main ( )
static int i;
i = i + 36.56;
printf ( “%d”, i);
extern int i;
int i = 10;
Best Books of C