🟡 Intermediate  ·  Lesson 29

Storage Classes

Storage Classes

What are Storage Classes?

Storage classes in C define a variable's scope, lifetime, default value and storage location.

Storage ClassScopeLifetimeDefault Value
autoBlockTill block endsGarbage
registerBlockTill block endsGarbage
staticBlock/FileWhole program0
externGlobalWhole program0

auto Storage Class

auto is the default storage class for local variables.

C Language
#include <stdio.h>

int main() {
    auto int x = 10;   // same as int x = 10;
    printf("x = %d", x);
    return 0;
}

register Storage Class

register requests the compiler to store a variable in CPU register for faster access. Modern compilers decide optimization themselves, so this keyword is rarely needed.

C Language
#include <stdio.h>

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

You cannot take the address of a register variable using &.

static Storage Class

A static local variable keeps its value between function calls.

C Language
#include <stdio.h>

void counter() {
    static int count = 0;
    count++;
    printf("Count = %d\n", count);
}

int main() {
    counter();
    counter();
    counter();
    return 0;
}
Count = 1 Count = 2 Count = 3

extern Storage Class

extern tells the compiler that a global variable is declared somewhere else, possibly in another file.

C Language
// file1.c
int total = 100;

// file2.c
#include <stdio.h>
extern int total;

int main() {
    printf("Total = %d", total);
    return 0;
}

Summary

  • auto is default for local variables
  • register is for fast access request
  • static preserves value for whole program lifetime
  • extern refers to a global variable defined elsewhere
  • Storage class controls scope and lifetime

Storage Classes क्या हैं?

C में storage classes variable की scope, lifetime, default value और storage location define करती हैं।

Storage ClassScopeLifetimeDefault
autoBlockBlock खत्म होने तकGarbage
registerBlockBlock खत्म होने तकGarbage
staticBlock/FileWhole program0
externGlobalWhole program0

auto

Local variables के लिए auto default storage class है।

C Language
auto int x = 10;   // same as int x = 10;

register

register compiler से request करता है कि variable को CPU register में रखे। Modern compilers खुद optimization करते हैं।

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

static

Static local variable function calls के बीच अपनी value preserve रखता है।

C Language
void counter() {
    static int count = 0;
    count++;
    printf("%d\n", count);
}

extern

extern बताता है कि variable की definition किसी दूसरी file या जगह पर है।

C Language
extern int total;
printf("%d", total);

सारांश

  • auto local variable का default है
  • register fast access की request करता है
  • static value को preserve रखता है
  • extern दूसरी जगह defined global variable को access करता है
  • Storage class scope और lifetime control करती है
← Back to C Tutorial