What are storage classes in C with example?
In C programming, storage classes are the way of defining the scope and lifetime of a variable within a program.
Types of Storage Classes in C
The four storage classes in C are:
- Auto Storage Class
- Register Storage Class
- Static Storage Class
- Extern Storage Class
Auto Storage Class
This is the default storage class for local variables defined within a function. They are stored on the stack and their lifetime is limited to the duration of the function call. Once the function call is complete, the memory allocated to the variable is released.
Here is an example of using the auto
storage class in C:
#include <stdio.h>
int main()
{
print_number();
return 0;
}
void print_number()
{
auto int x = 5; // x is an auto variable with initial value of 5
printf("The value of x is %d", x);
}
In this program, the variable x
is declared within the print_number()
function as an auto
variable with an initial value of 5. The printf
statement inside the function prints the value of x
. When the print_number()
function is called in the main()
function, the output would be "The value of x is 5".
It's important to note that the variable x is only available within the scope of the function print_number
and once the function call is complete, the memory allocated to the variable is released.
Register Storage Class
In contrast to RAM, variables specified using the register storage class are kept in a register. As access to registers is quicker than access to memory, this could enhance performance. Not all variables, however, can be saved in registers because there are only a certain amount of them available.
Here is an example of using the register
storage class in C:
#include <stdio.h>
int main()
{
register int x = 5; // x is a register variable with initial value of 5
printf("The value of x is %d", x);
return 0;
}
n this program, the variable x
is declared as a register
variable with an initial value of 5. The printf
statement in the main()
function prints the value of x
. When the program is run, the output would be "The value of x is 5".
It is important to note that the use of register
storage class is only a suggestion to the compiler, not a requirement. Compiler may or may not store the variable in register based on the availability of registers.
Also, It's important to note that variables declared as register
should be accessed very frequently in the program and should not have large size as it will affect the performance.
Static Storage Class
Variables declared with the static
storage class retain their value between function calls and have a scope that is limited to the file in which they are defined. They have a longer lifetime than auto
variables.
Here is an example of using the static
storage class in C:
#include <stdio.h>
void increment_number();
int main()
{
increment_number();
increment_number();
increment_number();
return 0;
}
void increment_number()
{
static int x = 0; // x is a static variable with initial value of 0
x++;
printf("The value of x is %d", x);
}
In this program, the variable x
is declared within the increment_number()
function as a static
variable with an initial value of 0. The function increments the value of x
by 1 each time it is called and then prints the value of x
.
When the increment_number()
function is called three times in the main()
function, the output would be:
The value of x is 1
The value of x is 2
The value of x is 3
It's important to note that the variable x is stored in the memory and retains its value between function calls.
Extern Storage Class
The extern
storage class is a keyword that is used to indicate that a variable has been defined in another file, rather than in the current file.. In C, global variables are variables that are defined outside of any function or block. They are also called external variables. They can be accessed from any function or block in the program, regardless of where they are defined.
Here is an example of using the extern
storage class in C:
File: main.c
#include <stdio.h>
int main()
{
extern int x; // x is defined in another file
printf("The value of x is %d", x);
return 0;
}
File: other.c
int x = 5; // x is a global variable with initial value of 5
In this example, the variable x
is defined in the file other.c
as a global variable with an initial value of 5. In the file main.c
, the extern
keyword is used to indicate that x
is defined in another file. The printf
statement in the main()
function prints the value of x
.
When the program is compiled, the two files are compiled separately and then linked together. The linker takes the object files generated by the compiler and links them together into a single executable. The object file for main.c
contains the reference to the variable x
, which is then resolved by the linker to the definition of x
in the object file for other.c
.
It's important to note that the extern
keyword does not define a variable, it only declares it and tell the compiler that the variable exist somewhere else. Also, the storage class of the variable x
is determined by where it was defined, in this case it's global variable.