Storage Classes in C

Storage classes in C define the scope, lifetime, default value, and location of variables in memory. They decide how long a variable will stay alive and where it can be accessed.

What Are Storage Classes?

Each variable in C belongs to a particular storage class. The storage class determines:

Types of Storage Classes

C provides four major storage classes:

1. auto Storage Class

Default value: Garbage

2. register Storage Class

Used for: performance optimization

3. static Storage Class

Default value: 0

static is used to preserve state (Counter, cache, configuration values, etc.)

4. extern Storage Class

Common use: multi-file projects

Comparison Table

Storage  | Scope        | Lifetime      | Default Value | Memory
---------------------------------------------------------------
auto     | Local        | Function call | Garbage       | Stack
register | Local        | Function call | Garbage       |
register | Local        | Function call | Garbage       | CPU Register
static   | Local/Global | Entire program| 0             | Static/Global Segment
extern   | Global       | Entire program| 0             | Global Segment

Memory Layout Overview

πŸ“Œ auto & register β†’ short-lived (function-level) πŸ“Œ static β†’ long-lived (program-level) πŸ“Œ extern β†’ globally accessible

Examples for Each Storage Class

Example 1 – auto Variable

#include <stdio.h>

int main() {

    auto int x = 10;     // auto is optional
    printf("x = %d", x);

    return 0;
}

Example 2 – register Variable

#include <stdio.h>

int main() {

    register int i;
    for(i = 1; i <= 5; i++)
        printf("%d ", i);

    return 0;
}

Example 3 – static Variable (Value Retains)

#include <stdio.h>

void counter(){
    static int c = 1;   // retains value
    printf("%d ", c);
    c++;
}

int main(){
    counter();
    counter();
    counter();
    return 0;
}

Example 4 – extern Variable (Used Across Files)

// File1.c
int num = 50;
// File2.c
#include <stdio.h>

extern int num;

int main(){
    printf("%d", num);
    return 0;
}

Real-Life Uses of Storage Classes

Storage Classes in C – 15 Practical Examples

1. auto – Default Local Variable

#include <stdio.h>

int main(){
    auto int a = 10;
    printf("%d", a);
    return 0;
}

2. auto – Implicit Auto

#include <stdio.h>

int main(){
    int x = 5;   // auto by default
    printf("%d", x);
    return 0;
}

3. auto – Local Block Storage

#include <stdio.h>

int main(){
    {
        auto int a = 20;
        printf("%d", a);
    }
    return 0;
}

4. static – Preserve Value

#include <stdio.h>

void counter(){
    static int c = 0;
    c++;
    printf("%d ", c);
}

int main(){
    counter();
    counter();
    counter();
    return 0;
}

5. static – One Time Initialization

#include <stdio.h>

void show(){
    static int x = 100;
    printf("%d\n", x);
    x++;
}

int main(){
    show();
    show();
    return 0;
}

6. static – Inside Loop

#include <stdio.h>

int main(){
    for(int i=0; i<3; i++){
        static int num = 0;
        num++;
        printf("%d ", num);
    }
    return 0;
}

7. static – Used for Caching

#include <stdio.h>

int cached(){
    static int value = 50;
    return value;
}

int main(){
    printf("%d", cached());
    return 0;
}

8. extern – Access Global Variable

#include <stdio.h>

int x = 100;

void show(){
    extern int x;
    printf("%d", x);
}

int main(){
    show();
    return 0;
}

9. extern – Modify Global Variable

#include <stdio.h>

int value = 10;

void update(){
    extern int value;
    value += 5;
}

int main(){
    update();
    printf("%d", value);
    return 0;
}

10. extern – Multi-function Usage

#include <stdio.h>

int g = 25;

void fun1(){
    extern int g;
    g++;
}

void fun2(){
    extern int g;
    g += 2;
}

int main(){
    fun1();
    fun2();
    printf("%d", g);
    return 0;
}

11. register – Faster Variable Access

#include <stdio.h>

int main(){
    register int i;
    for(i=1; i<=5; i++){
        printf("%d ", i);
    }
    return 0;
}

12. register – Loop Optimization

#include <stdio.h>

int main(){
    register int count = 0;
    count++;
    printf("%d", count);
    return 0;
}

13. register – Cannot Use Address

#include <stdio.h>

int main(){
    register int n = 10;
    // printf("%p", &n); // not allowed
    printf("%d", n);
    return 0;
}

14. static Global – File Scope

#include <stdio.h>

static int num = 50;

int main(){
    printf("%d", num);
    return 0;
}

15. static Local + extern Global Combined

#include <stdio.h>

int g = 10;

void test(){
    static int s = 5;
    extern int g;
    s++;
    g++;
    printf("%d %d\n", s, g);
}

int main(){
    test();
    test();
    return 0;
}

Important Notes

Practice Questions

  1. What is a storage class? List all types.
  2. Difference between auto and register variables.
  3. Why do static variables preserve their value?
  4. Write a program demonstrating extern storage class.
  5. Where are static variables stored in memory?

Practice Task

Create 4 programs to demonstrate: βœ” auto variable βœ” register loop counter βœ” static counter that increases each call βœ” extern global variable shared between two files

End of Storage Classes