In C, two concepts govern where a variable can be accessed (scope) and how long it exists in memory (lifetime): Scope and Storage Classes.
1. Variable Scope
Scope defines the region of the program where a declared variable is recognized. There are three main types of scope:
A. Local (Block) Scope
A variable declared inside a function or inside a specific code block (defined by curly braces {}) is a local variable.
- Visibility: It is only visible and accessible within that specific block.
- Example: Variables declared inside the
main()function or inside aforloop are local.
void my_function() {
int local_var = 10; // Local scope
}
int main() {
// local_var is NOT accessible here
int another_local = 20; // Local scope to main()
return 0;
}
B. Global Scope
A variable declared outside of all functions is a global variable.
- Visibility: It is visible and accessible from any function in the program.
- Lifetime: It exists for the entire duration of the program.
int global_counter = 0; // Global scope
void increment() {
global_counter++; // Accessible here
}
2. Storage Classes
The storage class defines two properties of a variable: its scope (visibility) and its lifetime (how long it remains in memory). This is specified using a keyword before the data type.
| Class | Keyword | Scope | Lifetime | Initial Value (Default) | Memory Location |
| Automatic | auto | Local (Block) | Until the block ends | Garbage (unpredictable) | Stack |
| Register | register | Local (Block) | Until the block ends | Garbage | CPU Registers (if available) |
| Static | static | Local or Global | Program’s Duration | 0 (zero) | Data Segment |
| External | extern | Global | Program’s Duration | 0 (zero) | Data Segment |
A. auto
This is the default storage class for local variables. It is rarely written explicitly. When the function or block finishes, the memory allocated on the Stack is released.
B. static
The static keyword can be used on local or global variables, dramatically changing their behavior.
Local Static Variable: A local variable declared with static keeps its value between function calls. It is initialized only once and is not destroyed when the function exits.
void counter() {
static int count = 0; // Initialized to 0 only the first time
count++;
printf("Function called %d times\n", count);
}
// If called twice, output is 1, then 2.
Global Static Variable: A global variable declared with static limits its visibility to only the current source file where it is defined, preventing other files from accessing it.
C. extern
The extern keyword is used to declare a global variable that is defined in another source file. It tells the compiler, “Trust me, this variable exists somewhere else; don’t allocate memory for it here.” This is essential for multi-file projects.
In file1.c (where the variable is defined):
int shared_data = 50;
In file2.c (where the variable is used):
extern int shared_data; // Declaration only, no memory allocated
void access_data() {
printf("Data from file1: %d\n", shared_data);
}
