Every variable has two properties in C language that are:
The Storage Class of a variable decides its scope, lifetime, storage location, and default value.
A variable’s storage location is either the CPU memory or the memory registers.
In C, there are four storage classes. Any variable in C belongs to one of these classes, shown below:
The storage class specifiers are the keywords that tell the compiler the variable’s storage class. These specifiers are declared, as shown below:
The four storage class specifiers are:
Suppose a storage class specifier is not declared. In that case, the variables inside the function body are auto
by default, variables outside the function body are static
, and the variables within a function are extern
.
A variable specified with the auto
specifier belongs to the automatic storage class. This variable has the following properties:
Storage Location | Lifetime | Scope | Initial Value |
---|---|---|---|
CPU Memory | Within the function body | Local | Garbage Value |
The variables defined within a function block are local to the function. The variable is not accessible outside the function block because it gets destroyed on the block’s exit.
A variable specified with the static
specifier belongs to the static storage class. This variable has the following properties:
Storage Location | Lifetime | Scope | Initial Value |
---|---|---|---|
CPU Memory | Retains the variable’s value | Local | Zero |
A static
variable will retain its value in between different function calls.
A variable specified with the register
specifier belongs to the register storage class. This variable has the following properties:
Storage Location | Lifetime | Scope | Initial Value |
---|---|---|---|
Register Memory | Within the function body | Local | Garbage Value |
The variables defined within a function block are local to the function. They are not accessible outside the function, similar to the auto
specifier. However, the register
variable gets stored in register memory instead of CPU memory, providing faster access to the variables.
A variable specified with the extern
specifier belongs to the external storage class. This variable has the following properties:
Storage Location | Lifetime | Scope | Initial Value |
---|---|---|---|
CPU Memory | Till the end of the program file | Global | Zero |
An extern
variable is defined anywhere in the file and is accessible anywhere in the main source file or others.
A variable that is not initialized but has allocated memory stores a garbage value.
The code below shows the declaration of storage class specifiers in C:
The auto
variable a
is defined and printed in the main
function.
The register
variable b
is defined and printed in the test_func
.
The extern
variable c
is defined outside the functions and printed in the main
function.
The static
variable d
is defined, modified, and printed inside the main
function.
#include <stdio.h>//extern storage class specifierextern int c;int c = 30;int test_func() {// register storage class specifierregister int b = 20;printf ("Value of b: %d\n", b);}int main() {//auto storage class specifierauto int a = 10;printf ("Value of a: %d\n", a);//Call the test_functest_func();printf ("Value of c: %d\n", c);//static storage class specifierstatic int d = 40;d = d - 5;printf ("Value of d: %d\n", d);return 0;}
Free Resources