📘 Lesson  ·  Lesson 59

Storage Classes

Storage class क्या है?

जब आप C में कोई variable declare करते हैं, तो compiler को उसके type से ज़्यादा जानने की ज़रूरत होती है। उसे पता होना चाहिए कि variable कहाँ इस्तेमाल हो सकता है, कितनी देर memory में रहेगा, और अगर आप value न दें तो किस value से शुरू होगा। Storage class इन्हीं तीन सवालों का जवाब देता है।

तो हर variable की तीन properties उसके storage class से जुड़ी होती हैं:

  • Scope — code का वह हिस्सा जहाँ variable दिखता है।
  • Lifetime — variable कितनी देर memory में रहता है।
  • Default value — initialise न करने पर उसमें क्या रहता है।

C इन्हें तय करने के लिए चार keyword देता है: auto, register, static और extern

चारों storage classes एक नज़र में

Keywordएक लाइन में
autoहर local variable का स्वचालित default।
registerVariable को तेज़ CPU register में रखने का संकेत।
staticCalls के बीच value रखता है; file में globals भी छिपाता है।
externदूसरी file में defined global variable को दर्शाता है।

अब हर एक को एक चलते हुए example के साथ देखते हैं।

auto — default local

Function के अंदर declare किया कोई भी variable अपने आप auto होता है। Keyword लिखना optional है और कुछ नहीं जोड़ता — इसीलिए असली code में यह लगभग नहीं दिखता।

C Language
#include <stdio.h>
int main() {
    auto int x = 10;   // वही जो: int x = 10;
    int y = 20;        // y भी default से auto है
    printf("x = %d, y = %d\n", x, y);
    return 0;
}
Output:
x = 10, y = 20
💡 ध्यान दें

auto variable block शुरू होने पर बनता है और block खत्म होने पर मिट जाता है। Initialise न करें तो इसमें garbage value होती है — हमेशा initialise करें।

register — speed का संकेत

register keyword request करता है कि variable RAM की जगह CPU register में रखा जाए, जो पढ़ने में तेज़ है। यह सिर्फ़ request है: compiler इसे अनदेखा कर सकता है। पहले इसका आम इस्तेमाल tight loop counters में होता था।

C Language
#include <stdio.h>
int main() {
    register int i;
    for (i = 1; i <= 3; i++)
        printf("%d ", i);
    return 0;
}
Output:
1 2 3
⚠️ ज़रूरी

Register variable का address नहीं ले सकते — &i error है। आधुनिक compiler इतना अच्छा optimise करते हैं कि register आज लगभग बेकार है।

static — value याद रखता है

यह सबसे उपयोगी storage class है। static local variable सिर्फ़ एक बार initialise होता है और calls के बीच अपनी value रखता है, क्योंकि इसका lifetime पूरा program होता है — एक call नहीं।

C Language
#include <stdio.h>
void counter() {
    static int count = 0;   // एक बार set, calls में बना रहता है
    count++;
    printf("Call number: %d\n", count);
}
int main() {
    counter();
    counter();
    counter();
    return 0;
}
Output:
Call number: 1
Call number: 2
Call number: 3

अगर count सामान्य local variable होता, तो हर call पर 0 हो जाता और हमेशा 1 print करता। static होना ही उसे गिनने देता है।

💡 अतिरिक्त उपयोग

static लगा global variable अपनी file तक सीमित हो जाता है — दूसरी files उसे extern से भी नहीं देख सकतीं। Internal helpers छिपाने का यह साफ़ तरीका है।

extern — files के बीच साझा

बड़े program कई .c files में बँटे होते हैं। जब एक file को ऐसा global variable चाहिए जो किसी दूसरी file में defined है, तो वह extern से कहती है कि "यह कहीं और मौजूद है।"

file1.c
#include <stdio.h>
int score = 100;        // असली definition

void show(void);        // file2.c से

int main() {
    show();
    return 0;
}
file2.c
#include <stdio.h>
extern int score;       // यहाँ declared, file1.c में defined

void show(void) {
    printf("Score = %d\n", score);
}
Output:
Score = 100
💡 नियम

extern सिर्फ़ declare करता है — memory कभी allocate नहीं करता। ठीक एक file को variable असल में define करना चाहिए (यहाँ file1.c)।

Scope, lifetime और default value

Storage classScopeLifetimeDefault value
autoLocal (block)Block खत्म होने तकGarbage
registerLocal (block)Block खत्म होने तकGarbage
staticLocal या fileपूरा program0
externGlobal (सभी files)पूरा program0

आम गलतियाँ

  • यह उम्मीद करना कि सामान्य local variable calls के बीच value याद रखेगा — इसके लिए static चाहिए।
  • Register variable का address & से लेने की कोशिश।
  • extern इस्तेमाल करके हर file में variable को initialise भी करना — सिर्फ़ एक file को define करना चाहिए।
  • यह मानना कि auto variable 0 से शुरू होते हैं — initialise होने तक इनमें garbage रहता है।
🏋️ अभ्यास

एक function int nextId() लिखें जो static counter से लगातार calls पर 1, 2, 3, … लौटाए। फिर static हटाकर देखें कि output कैसे बदलता है।

सारांश

  • Storage class किसी variable का scope, lifetime और default value तय करता है।
  • auto locals का default है — इसे लिखने की ज़रूरत नहीं।
  • register CPU-register storage का संकेत देता है; आज यह बेकार है।
  • static local की value calls के बीच रखता है और file में globals छिपाता है।
  • extern एक file को दूसरी file में defined global इस्तेमाल करने देता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में storage class असल में क्या control करता है?
Storage class किसी variable के बारे में तीन चीज़ें तय करता है: उसका scope (कहाँ दिखेगा), उसका lifetime (कितनी देर memory में रहेगा), और initialise न करने पर उसकी default value। चार storage classes हैं — auto, register, static और extern।
क्या आधुनिक C में auto की ज़रूरत होती है?
लगभग कभी नहीं। हर local variable पहले से ही auto होता है, इसलिए keyword लिखने से कुछ नहीं बदलता। C++11 और बाद में auto का मतलब बिल्कुल अलग (type deduction) है, इसलिए C में आप auto कभी नहीं लिखते।
Static local variable calls के बीच अपनी value क्यों याद रखता है?
क्योंकि static variable एक बार बनता है, program शुरू होने से पहले, और पूरे program के चलने तक रहता है — सिर्फ़ एक function call तक नहीं। Function उसे केवल अपने body के अंदर देख सकता है (local scope), पर return के बाद भी value बनी रहती है, इसलिए अगली call वहीं से आगे बढ़ती है।
static और extern में क्या अंतर है?
दोनों variable के साझा होने को प्रभावित करते हैं, पर उल्टे तरीके से। extern compiler को बताता है कि global variable किसी दूसरी file में defined है ताकि यह file उसे इस्तेमाल कर सके। File level पर static इसका उल्टा करता है — global variable को छिपा देता है ताकि दूसरी files उसे न देख सकें।
क्या register सच में program तेज़ करता है?
बहुत कम। register सिर्फ़ एक सुझाव है कि variable को CPU register में रखा जाए, और आधुनिक compiler पहले ही इससे बेहतर निर्णय ले लेते हैं। आप register variable का address & से नहीं ले सकते। असली code में यह लगभग बेकार हो चुका है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.