📦
Functions & Recursion

Storage Classes

auto, static, extern, register
💡 Storage classes batati hain ek variable "kaha rehta hai" aur "kab tak zinda rehta hai" — jaise auto ek hotel room hai (checkout hote hi khaali), static ek apna ghar hai (hamesha wahi rehta hai), extern ek doosre ghar ka pata sharing karna hai.

auto default storage class hai — normal local variables (explicitly likhna kabhi zaroori nahi, sab local variables automatically "auto" hote hain). Ye stack par rehte hain, function khatam hote hi gayab.

static do context mein use hota hai: local variable ke saath (value function calls ke beech preserve hoti hai, jaise counter), ya global variable/function ke saath (sirf usi file mein visible hota hai, doosri files mein access nahi kar sakte — "internal linkage").

extern kisi variable ko declare karta hai jo kisi DOOSRI file mein define hua hai — multi-file C programs mein global variables share karne ke liye use hota hai. register compiler ko "hint" deta hai ki ye variable bahut baar use hoga, CPU register mein rakho (modern compilers usually khud hi optimize kar lete hain, is keyword ki zaroorat kam padti hai).

int counter() {
  static int count = 0;  // sirf ek baar initialize hota hai
  count++;
  return count;
}

int main() {
  printf("%d\n", counter()); // 1
  printf("%d\n", counter()); // 2 — value preserve hui!
  printf("%d\n", counter()); // 3
  return 0;
}

// file2.c mein: extern int sharedVar; // file1.c mein defined variable use karo
📦
Storage classes batati hain ek variable "kaha rehta hai" aur "kab tak zinda rehta hai" — jaise auto ek hotel room hai (checkout hote hi khaali), static ek apna ghar hai (hamesha wahi rehta hai), extern ek doosre ghar ka pata sharing karna hai.
1 / 2
⚡ Quick Recap
  • auto = default, stack par, function khatam hote hi gayab
  • static = value preserve hoti hai calls ke beech (local) ya file tak limited scope (global)
  • extern = doosri file mein defined variable use karo
Is page mein (2 subtopics)

register keyword compiler ko suggest karta hai ki variable ko CPU register mein rakho (memory ke bajaye), taaki access bahut fast ho — usually loop counters ke liye jo bahut baar access hote hain. Modern compilers itne smart hain ki wo khud hi decide kar lete hain, isliye register manually likhna aajkal rare hai.

int sum(int arr[], int n) {
  register int i;   // hint: 'i' bahut baar use hoga
  int total = 0;
  for (i = 0; i < n; i++) {
    total += arr[i];
  }
  return total;
}
💡Tip: register variable ka address nikalna (&i) INVALID hai — registers ki koi memory address nahi hoti. Ye ek common compile error hai jab log register ke saath & use karte hain.

Agar ek local variable ka naam kisi global variable jaisa hi ho, local variable "shadow" kar deta hai global ko us scope ke andar — global ko access karna ho to explicit scope operators C mein available nahi hain (C++ ke :: jaisa), isliye naming clash se bachna hi best practice hai.

int x = 100;  // global

void func() {
  int x = 5;   // local 'x' shadows global 'x'
  printf("%d\n", x);  // 5 (local wins)
}

int main() {
  func();
  printf("%d\n", x);  // 100 (global unaffected)
  return 0;
}
⚠️Common Mistake: Global aur local variables ke same naam rakhna confusing bugs ka source hai — jaanbujh kar alag naming conventions use karo (jaise globals ke liye g_ prefix) taaki shadowing accidentally na ho.