Storage Classes in C
Storage Classes in C
The storage class of a variable in C determines its scope, visibility, and lifetime. The storage
class specifies how and where the variable is stored. There are four storage classes in C:
1. auto: This is the default storage class for local variables. Variables declared with the
auto keyword (or without any storage class specifier) are stored in the stack, have
automatic storage duration, and are local to the block in which they are defined.
Example : -
#include <stdio.h>
void exampleFunction() {
auto int num = 10; // Explicitly declaring an auto variable
int count = 20; // Implicitly an auto variable (default)
printf("num: %d\n", num);
printf("count: %d\n", count);
}
int main() {
exampleFunction();
return 0;
}
In this example:
Both variables have automatic storage duration, meaning they are created when the function
exampleFunction is called and destroyed when the function returns.
2. register: This storage class suggests that the variable be stored in a CPU register instead
of RAM for faster access. Lifetime is limited to the block they are defined in. Variables
declared with register cannot have their address taken using the address-of operator
(&), as they may not have a memory address.
Example:-
#include <stdio.h>
int main() {
register int num = 10; // Suggesting to store the variable in a register
register int count = 20; // Another register variable
printf("num: %d\n", num);
printf("count: %d\n", count);
return 0;
}
In this example:
● num and count are declared with the register keyword, suggesting that they should be
stored in CPU registers if possible.
● These variables have automatic storage duration and are local to the main function.
● The variables cannot have their addresses taken.
3. Static
● The static variable is defined using the static keyword.
● Its scope depends the area of its declaration
● If a static variable is defined within a function, it is a local variable. If declared outside
the function, its scope is global.
● However, unlike regular local variables, the value of a static variable persists between
function calls.
● The default value of static variable is zero.
Example:-
#include<stdio.h>
void value()
{
int a=10;
static int b=20;
a=a+10;
b=b+10;
printf(“The value of local variable: %d \n”,a);
printf(“The value of static variable : %d\n”,b);
}
Int main()
{
value();
printf(“Calling function second time\n”);
value();
printf(“Calling function 3rd time\n”);
value();
return 0;
}
Output
The value of local variable: 20
The value of static variable : 30
Calling function second time.
The value of local variable: 20
The value of static variable : 40
Calling function 3rd time
The value of local variable : 20
The value of static variable : 50
4. extern
● The extern storage class in C is used to declare variables and functions that are
defined in another file or in a different scope within the same file.
● It provides a way to access global variables and functions across multiple files in
a project.
● Declaration: Using extern declares a variable without defining it. This means you tell
the compiler about the existence and type of the variable, but you don't allocate storage
for it.
● Definition: The actual allocation of storage for the variable happens in its definition,
usually in another file.
Scope:
● Variables declared with extern have global scope, meaning they can be accessed across
multiple files.
Usage:
● extern is used when you need to access a global variable defined in another file. It
helps in sharing variables across different files in a multi-file project.
Example:
#include <stdio.h>
int count = 10; // Definition of the global variable
void displayCount() {
printf("Count in displayCount: %d\n", count);
}
#include <stdio.h>
// Declare the external variable defined in File1.c
extern int count;
void incrementCount() {
count++;
}
int main() {
printf("Initial count in main: %d\n", count); // Prints the initial value of
count
incrementCount();
printf("Count after increment in main: %d\n", count); // Prints the
incremented value of count
displayCount(); // Calls the function from File1.c to display the updated
count
return 0;
}
In this example: