Contents

Storage Classes in C

Introduction to Storage Classes

Storage classes define the scope, lifetime, and visibility of variables/functions in C. They determine where a variable is stored (memory/register) and how long it persists during program execution.

Types of Storage Classes

1. Automatic (`auto`)

Properties:

  • Default storage class for local variables (inside functions/blocks).
  • Stored in stack memory.
  • Initial value: Garbage (undefined).
  • Scope: Local to the block where declared.
  • Lifetime: Until the block exits.

Example:

#include <stdio.h>
void demo() {
    auto int x = 10; // Explicitly declared as auto (optional)
    printf("%d\n", x);
}
int main() {
    demo(); // Output: 10
    return 0;
}

2. Register (`register`)

Properties:

  • Requests the compiler to store the variable in a CPU register (not guaranteed).
  • Used for frequently accessed variables (e.g., loop counters).
  • No address operator (`&`) can be applied (registers have no memory address).
  • Scope and lifetime: Same as `auto`.

Example:

#include <stdio.h>
int main() {
    register int i; // Suggests storing 'i' in a register
    for (i = 0; i < 1000; i++) {
        printf("%d\n", i);
    }
    return 0;
}

3. Static (`static`)

Properties:

  • Retains its value between function calls.
  • Stored in static memory (data segment).
  • Initial value: Zero (if uninitialized).
  • Scope: Local to the block/file (depends on declaration).
  • Lifetime: Entire program execution.

Example (Static Local Variable):

#include <stdio.h>
void counter() {
    static int count = 0; // Retains value across calls
    count++;
    printf("Count: %d\n", count);
}
int main() {
    counter(); // Count: 1
    counter(); // Count: 2
    return 0;
}

Example (Static Global Variable):

#include <stdio.h>
static int hidden = 5; // Only visible in this file

void print_hidden() {
    printf("%d\n", hidden);
}

4. External (`extern`)

Properties:

  • Used to declare a global variable/function defined in another file.
  • Stored in global memory (data segment).
  • Initial value: Zero (if uninitialized).
  • Scope: Across files (with proper linkage).
  • Lifetime: Entire program execution.

Example:

File `file1.c`:

#include <stdio.h>
int global_var = 42; // Definition

File `file2.c`:

#include <stdio.h>
extern int global_var; // Declaration (no memory allocated)
int main() {
    printf("%d\n", global_var); // Output: 42
    return 0;
}

5. Typedef (`typedef`)

Note:

  • Not a true storage class. Used to create aliases for existing types.
  • Example:
typedef int myInt; // myInt is now an alias for int
myInt x = 10;

Comparison of Storage Classes

Storage ClassStorage LocationDefault ValueScopeLifetime
`auto`StackGarbageBlockBlock
`register`CPU RegisterGarbageBlockBlock
`static`Data SegmentZeroBlock/FileProgram
`extern`Data SegmentZeroGlobal (Files)Program

Key Takeaways

  • Use `static` to preserve values between function calls.
  • Use `extern` to share variables/functions across files.
  • `register` is a hint to the compiler (no guarantee).
  • `auto` is redundant (default for local variables).