Storage Classes in C Language:
C Storage classes provide the following information to the compiler
- Storage area of a variable.
- Scope of variable i.e. in which block the variable is visible.
- The lifetime of a variable i.e. how long the variable will be there in active mode.
- The default value of a variable if it is not initialized.
How many types of storage classes are there in the c programming language?
Depending on the behavior and storage area storage classes are classified into two types, such as
- Automatic storage class
- Static storage class
What is the automatic storage class?
This storage class variables will be created automatically and destroyed automatically. Automatic storage class variables will be store in the stack area of the data segment or in the CPU register. Under automatic storage class, we have two types of storage class specifiers, such as
- Auto
- register
What is the static storage class?
Static storage class variables will be created only once and throughout the program, it will be there in active mode. Static storage class variables will be there in the static area of the data segment. Under static storage class, we have two types of storage class specifiers
- Static
- extern
Please have a look at the following diagram for the classification of C storage classes.
Auto Storage Class in C Language:
The variable which is created by using the specifier auto within a block is called auto variable. The auto storage class is the default storage class for all local variables. Let us see a program for better understanding of the Auto Storage Class in C Language.
#include <stdio.h> void abc(); int main() { abc(); abc(); abc(); return 0; } void abc() { auto int a = 5; ++a; printf("\n a = %d ", a); }
Output:
The lifetime of the auto variable is restricted within the body that’s why the number of times we are calling the function that many times it will be created again and again. Let us see another program for more understanding.
#include <stdio.h> void abc(); int main() { auto int a = 5; ++a; abc(); abc(); ++a; printf("\n a = %d", a); return 0; } void abc() { int a = 10; ++a; printf("\n a = %d ", a); }
Output:
According to the storage class of c scope of the auto variable is restricted within the body only that’s why abc() function related data can’t be access in main() function. Let us see a program to understand this concept.
#include <stdio.h> void abc(); int main() { abc(); abc(); printf("\n a = %d", a); return 0; } void abc() { int a = 10; ++a; printf("\n a = %d ", a); }
Output:
Register Storage Class in C Language:
It is a special kind of variable that stores in the CPU register. The advantages of register variables are faster than the remaining 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).
Example to Understand Register Storage Class in C Language
#include <stdio.h> int main() { register int a = 10; ++a; printf("Value of a : %d", a); printf("\nEnter a value"); scanf("%d" , a); --a; printf("\n Value of a : %d", a); return 0; }
Output:
Value of a : 11
Enter a value 30
Value of a : 29
Note: register storage class specifier first recommended to the compiler to hold the variable in CPU register if the memory is available or else store in stack area of the data segment.
In scanf() function if the address is provided for the register variable then it will give an error, if the address is not provided then it works normally.
Example to Understand Register Storage Class in C Language
#include <stdio.h> int main() { register int a = 10; ++a; printf("Value of a : %d", a); printf("\nEnter a value"); scanf("%d" , &a); --a; printf("\n Value of a : %d", a); return 0; }
Output:
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.
Limitations of Register Variables in C Language:
When we are working with register variables we can’t create “n” number of register variables i.e. depending upon CPU capacity 4-6 are maximum variables we can create. In TC 3.0 we cannot access the address of register variables (but we can access in GCC and dev c). Pointer or pointer related concepts cannot be applied to register variables (in TC 3.0).
Static Storage Class in C Language:
The variable which is created by using the specifier static is called as static variable. When we are working with the auto variables, scope, and lifetime both are restricted with the body. But when we are working with static variables, the scope is restricted is within the function but the lifetime is not restricted.
The static storage class instructs the compiler to keep a local variable in existence during the lifetime 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.
Program to Understand Static Storage Class in C Language
#include <stdio.h> /* function declaration */ void func(void); static int count = 5; /* global variable */ int main() { while(count--) { func(); } return 0; } /* function definition */ void func( void ) { static int i = 5; /* local static variable */ i++; printf("i is %d and count is %d\n", i, count); }
Output:
Extern Storage Class in C Language:
The variable which is created by using the keyword extern called as extern variable. In implementation when we need to access a variable in more than one function then go for extern variables i.e global variables are required. When we are working with global variables scope and lifetime both are not restricted. Whenever we are declaring a variable outside the function then it is called as global variable.
The extern storage class is used to give a reference to a global variable that is visible to ALL the program files. When you use ‘extern’, the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declaring a global variable or function in another file.
The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
First File: main.c
#include <stdio.h> int count; extern void write_extern(); main() { count = 5; write_extern(); }
Second File: support.c
#include <stdio.h> extern int count; void write_extern(void) { printf("count is %d\n", count); }
Here, extern is being used to declare count in the second file, whereas it has its definition in the first file, main.c. Now, compile these two files as follows –
$gcc main.c support.c
It will produce the executable program a.out. When this program is executed, it produces the following result − count is 5
Summary of Storage Classes in C Language:
Note: By default any kind of storage class specifier is auto. In c programming language there are four types of scopes are available i,e, body, function, program, and file scope.