Every variable in C has a storage class that dictates four of its characteristics:
· Scope: Where in the code the variable is accessible.
· Lifetime: The duration for which the variable remains in memory.
· Storage: The location where the variable is stored (main memory or CPU registers).
· Default Initial Value: The value it holds if not explicitly initialized.
There are four
storage class specifiers in C: auto
, extern
, static
, and register
.
1. auto
(Automatic) 🚗
This is the default storage class for all local variables (variables declared inside a function or a block).
· Purpose: For variables that are local to a function and only needed during its execution.
·
Keyword: auto
(rarely used, as it's the default).
·
Scope: Block scope. It is only accessible within the
block {...}
where it is defined.
· Lifetime: It is created when the block is entered and destroyed when the block is exited.
· Storage: Stack memory.
· Default Value: Garbage value (unpredictable).
Example
C
#include <stdio.h>
void myFunction() {
// Both 'x' and 'y' have the auto storage class.
int x =
10;
auto
int y =
20;
// The 'auto' keyword is redundant here.
printf(
"Inside myFunction: x=%d, y=%d\n", x, y);
}
int main() {
myFunction();
// printf("%d", x); // COMPILE ERROR: 'x' is not accessible here (out of scope)
return
0;
}
size=2 width="100%" align=center>
2. extern
(External) 🌍
The extern
keyword is used to declare a global variable,
telling the compiler that this variable is defined (created) in another
source file.
·
Purpose: To share a global variable across multiple .c
files.
·
Keyword: extern
·
Scope: Global scope. Accessible from any file that
includes its extern
declaration.
· Lifetime: Exists for the entire duration of the program.
· Storage: Data segment of memory.
· Default Value: Zero.
Example
main.c
:
C
#include <stdio.h>
// This declares that a global variable named 'global_var' exists
// somewhere else and we want to use it here.
extern
int global_var;
int main() {
printf(
"The value of the global variable is: %d\n", global_var);
return
0;
}
support.c
:
C
// This is the actual definition of the global variable.
// Memory is allocated for it here.
int global_var =
100;
To
Compile: gcc main.c
support.c -o myprogram
3. static
🔒
The static
keyword has two different meanings depending on its
context.
a) Static Local Variable
When used inside a
function, static
changes the variable's lifetime from a single
function call to the entire program's execution. It retains its value between
function calls.
· Scope: Block scope (still only accessible inside the function).
· Lifetime: The entire program's duration.
· Default Value: Zero.
Example:
C
#include <stdio.h>
void counter() {
// 'count' is initialized only once and retains its value.
static
int count =
0;
count++;
printf(
"This function has been called %d time(s).\n", count);
}
int main() {
counter();
// Prints 1
counter();
// Prints 2
counter();
// Prints 3
return
0;
}
b) Static Global Variable
When used on a
global variable, static
restricts the variable's visibility to the single
file in which it is defined. It cannot be accessed from other files using extern
.
· Scope: File scope.
· Lifetime: The entire program's duration.
Example: main.c
:
C
#include <stdio.h>
// extern int file_scope_var; // This would cause a LINKER ERROR because
// 'file_scope_var' is static in the other file.
int main() {
printf(
"This demonstrates a static global variable.\n");
return
0;
}
support.c
:
C
// This variable is only accessible within support.c
static
int file_scope_var =
50;
size=2 width="100%" align=center>
4. register
🏃♂️
The register
keyword is a hint to the compiler to store a
local variable in a fast CPU register instead of in main memory (RAM).
· Purpose: To suggest a variable for optimization because it is frequently accessed.
·
Keyword: register
·
Important
Note: Modern compilers are excellent
at optimization and will often ignore this keyword, making better decisions on
their own about what to place in a register. You also cannot take the address (&
) of a register variable.
·
Scope,
Lifetime, Default Value: Same as auto
.
Example
C
#include <stdio.h>
int main() {
// Requesting that 'i' be stored in a CPU register for the loop.
register
int i;
for (i =
0; i <
10000; i++) {
// ... some frequent operation
}
printf(
"Loop finished.\n");
return
0;
}
size=2 width="100%" align=center>
Summary Table
Storage Class |
Scope |
Lifetime |
Storage |
Default Value |
|
Block |
End of block |
Stack |
Garbage |
|
Global |
Entire program |
Data Segment |
Zero |
|
Block / File |
Entire program |
Data Segment |
Zero |
|
Block |
End of block |
CPU Register |
Garbage |
Storage classes in C do more than define a variable's lifetime and scope; they fundamentally control its linkage—how its name is shared across different files—and its placement within the program's memory.