Storage class in C language defines the location, lifetime and other specific attributes of a variable. Knowledge of the storage class is important in accessing a block of code. A basic understanding of storage classes in the C language can help you get a job as a computer programmer. In this article, we explain what storage classes are and discover their types and examples.
What are storage classes in C language?
Storage classes in C language decide which portion of the program recognises the variables. A variable in C language is used to store data and is stored in a physical location in the random memory. Each C language variable has a storage class that determines the life of a variable.
Storage classes are used to determine how long the storage allocation continues to exist. They also determine the part of memory where storage is allocated for an object. Storage classes in C determine if the variable is local or global. Storage class can determine from which part of the code we can access the variable.
Types of storage classes in C language
Here are the four types of storage classes in C programming:
- Automatic storage class
- Register storage class
- Static storage class
- External storage class
The automatic storage class (auto)
The automatic storage classes are the default storage for all local variables. For the automatic storage class, the storage area is the main memory and it has an automatic lifetime. Initial or default value here is unpredictable or garbage value. The scope of the variable in this data is a local scope or block scope.
Here, the keyword ‘auto’ is used to specify the storage class of a variable. The variables which are declared within the body of any function are automatic variables. This is why there is no need to explicitly define them as the keyword auto is optional.
Here are the examples of an automatic storage class:
Example 1:
#include<stdio.h>
int main() {
Increment();
Increment();
Increment();
}
Increment(){
auto int i=1;
printf(“%d”,i);
i = i +1
}
Output: 1 1 1
Example 2:
- #include <stdio.h>Â Â
- int main()Â Â
- {Â Â
- int a = 10,i;Â Â Â
- printf(“%d “,++a);Â Â
- {Â Â
- int a = 20;Â Â Â
- for (i=0;i<3;i++)Â Â
- {Â Â
- printf(“%d “,a); // 20 will be printed 3 times since it is the local value of a Â
- }Â Â
- }Â Â
- printf(“%d “,a); // 11 will be printed since the scope of a = 20 is ended.  Â
- }Â Â
Output: 11 20 20 11
The register storage class (register)
The register storage class is used in defining variables that are allocated in the CPU registers instead of RAM depending on hardware and implementation restrictions. A local register variable is defined with the keyword ‘register’. Register variable is different from the auto variable only in terms of storage area. Register storage class only specifies local variables.
Default value of the register variable is the garbage value. Both auto and register variables have similar scope and lifetime. Register variables have an initial default value of zero. Register variables can be faster than the automatic variables.
Here are the examples of a register storage class:
Example 1:
#include<stdio.h>
int main() {
register int i=2;
printf(“%d”,i);
}
Output: 2
Example 2:
register int miles;
Static storage class (static)
Static storage classes use the keyword ‘static’ as they deserve values after their scope is ended. Static variables can hold their value between the multiple function calls. Here the lifetime of static variables is until program execution. The default value of the static variable is zero.
Static storage classes are used on global variables. Static variables can be accessed anywhere and anytime in C programming. As long as the program is under execution, the static variables remain preserved.
Here are some examples of static storage classes in C programming:
Example 1:
#include<stdio.h>
void increment()
{
static int i;
i++;
printf(“%d\t”,i);
}
int main()
{
increment();
increment();
increment();
return 0;
}
Output: 1 2 3
Example 2:
#include<stdio.h>
#include<conio.h>
int main() {
test();
test();
}
int test() {
static int i=10;
i = i + 1;
printf(“\n%d”,i);
}
Output:10 11
External storage class (extern)
External storage classes in C language are used when a variable is defined somewhere else but is used in a block or a function. Extern is used to specify the global variables. Keyword extern precedes the declaration of these global variables. Global variables are generally defined outside of all functions.
The variables defined by the external storage class can be accessed by any function within the program. This is why they have global scope. The default initial value of these variables is zero. The value of these global variables remains as long as the program is under execution.
Here are some examples of an external storage class in C language:
Example 1:
#include <stdio.h>
int a = 10;
int main( )
{
extern int b;
printf(“Value of a = %d and b = %d \n”, a, b);
return 0;
}
int b = 20;
Output: Value of a = 10 and b = 20
Example 2:
/variable.c
extern int i; //refer i in program1.c
void show()
{
int j;
j=i*2;
printf(“\nValue of i in variable.c = %d”, j);
}
By defining the main function inside program1.c
#include<stdio.h>
#include “variable.c” //link program variable.c
int i; //external global declaration
void show();
int main()
{
i=10;
show();
printf(“\nValue of i in program1.c = %d”, i);
return 0;
}
Output:
value of i in variable.c = 20
value of i in program1.c = 10
Important terms related to storage classes
Storage classes determine the properties of a variable which are scope, lifetime, initial value and lifetime. These features tell about the storage of variables and their default values. Here are some important terms related to storage classes in C programming:
Scope of a variable
Scope is used to access the variable in a program. It determines the exact part of the program where variables exist. Variables only exist in the scope. They cannot be accessed beyond the scope.
Variables can exist in three different positions in the C programming language. These are:
- Local variables: The variables declared inside a particular function or block are called local variables. Local variables are declared using the local declaration. These variables can only be used inside the block or function they are declared. Â
- Global variables: The variables declared outside a particular function or block are called global variables. Global declaration declares the global variables. Global variables can be accessed anywhere in the program.Â
- Formal parameters: The variables declared in the function definition are called formal parameters. Formal parameters are accessed only inside the function.Â
Block of code
In C programming language, a block of code is a few lines of code that are created using curly braces. A block of code begins with an open curly brace and ends with a closing curly brace. It is responsible for collecting different statements to create a compound statement.
Lifetime of a variable
Lifetime of a variable in the C programming language tells the duration in which memory is reserved during the execution of a program. A variable gets stored in the memory during its lifetime.
The computer allocates memory for a variable during this time. Lifetime of a variable is also known as the storage duration or allocation method. Variables can have the following three types of lifetime in C programming:
- Static lifetime: Variables with static lifetime are created for the first time in a program. This variable is destroyed when the execution of a program stops. The memory of a variable with static lifetime deallocates when the simulation ends.Â
- Automatic lifetime: Variables with automatic lifetime are created whenever the execution of that block gets destroyed. The variables with automatic lifetime are stored in a function call stack.Â
- Dynamic lifetime: Variables with dynamic lifetime are created when the memory is allocated for the object. These variables end when the memory is deallocated using delete. Variables with dynamic functions are stored in the heap.Â
Garbage value of a variable
Garbage value is the default initial value of any variable in the C programming language. A garbage value or default initial value is assigned to a variable whenever the variable is declared in C. Different storage classes in the C language give different garbage values to the variables. For example, the garbage value in the auto storage class is unpredictable while in the static storage class it is zero. Initialisation can help in getting rid of the garbage value.


