The storage classes in C specify the scope (visibility) and lifetime (duration of existence) of a variable or function.
There are four main storage classes in C:
1. auto (Automatic) 🕒
Default: This is the default storage class for variables declared inside a function or block.
4 Scope: Local (only visible within the block where it's defined).
Lifetime: Automatic—the variable comes into existence when its block is entered and is destroyed when the block is exited.
5 Storage: Usually allocated on the stack.
6 Initialization: Garbage value (whatever data was previously in that memory location) unless explicitly initialized.
void myFunction() {
auto int x = 10; // 'auto' is usually omitted as it's the default
} // 'x' is destroyed here
2. extern (External) 🌍
Purpose: Used to declare a variable or function that is defined elsewhere (usually in another source file or later in the same file).
7 It tells the compiler, "Trust me, this variable exists, just reserve space for a reference to it now."Scope: Global (visible throughout the entire program).
8 Lifetime: Persists for the entire duration of the program execution.
9 Storage: Stored in the data segment (or BSS segment) of memory.
Initialization: Automatically initialized to zero by default if not explicitly initialized.
File 1: main.c
extern int global_counter; // Declaration: tells compiler it exists elsewhere
int main() {
printf("%d\n", global_counter); // OK
return 0;
}
File 2: support.c
int global_counter = 50; // Definition: actual storage is allocated here
3. static (Static) ♾️
The behavior of static changes depending on where it is used:
A. Static Local Variables (Inside a function)
Scope: Local (only visible inside the function).
Lifetime: Persists for the entire duration of the program, even after the function exits.
Initialization: Initialized only once when the program starts (or the first time the function is called, depending on context/compiler), retaining its value between calls.
10 Storage: Stored in the data segment.
B. Static Global Variables or Functions (Outside any function)
Scope: Restricted to the file (compilation unit) in which it is declared. It cannot be accessed or "seen" by other files, even if declared with
externelsewhere.
void count_calls() {
static int count = 0; // Retains value across calls
count++;
printf("Called %d times\n", count);
}
4. register (Register) ⚡
Purpose: A suggestion to the compiler that the variable will be frequently accessed. The compiler may try to store it in a fast CPU register instead of main memory to speed up access.
11 Scope: Local.
Lifetime: Automatic (local to the block).
Storage: CPU Register (if available) or main memory (if register isn't available).
12 Constraint: You cannot take the address (using the
&operator) of aregistervariable because registers do not have a conventional memory address.
void calculate() {
register int i; // Suggests 'i' should be kept in a fast register
for (i = 0; i < 1000; i++) {
// ... loop operations
}
}
Summary Table
| Storage Class | Keyword | Scope (Visibility) | Lifetime (Duration) | Initial Value | Storage Location |
| Automatic | auto | Within the block/function | Until block exit | Garbage | Stack |
| External | extern | Global (entire program) | Entire program execution | Zero | Data Segment |
| Static | static | Local: Block; Global: File | Entire program execution | Zero | Data Segment |
| Register | register | Within the block/function | Until block exit | Garbage | CPU Register (Preferred) |
To help visualize how storage location (Stack vs. Data Segment) affects variables, I'll search for a diagram of the C memory layout.
Would you like an example demonstrating the difference between a local auto variable and a static local variable inside the same function?
0 件のコメント:
コメントを投稿