🟡 Intermediate  ·  Lesson 29

Storage Classes

Scope और lifetime: दो बड़े विचार

C में हर variable की value के अलावा दो गुण होते हैं: scope (कहाँ इस्तेमाल हो सकता है) और lifetime (memory में कितनी देर रहता है)। Storage classes बस वे चार keywords हैं जो इन दो चीज़ों को नियंत्रित करते हैं।

यह lesson रोज़मर्रा के उदाहरणों से समझ बनाता है। संक्षिप्त reference-शैली version के लिए C में storage classes भी देखें।

विचारयह किसका उत्तर देता है
Scopeयह variable कहाँ इस्तेमाल कर सकता हूँ?
Lifetimeयह कितनी देर अपनी value रखता है?

auto: रोज़मर्रा का default

हर सामान्य local variable auto है — आपको यह शब्द कभी टाइप नहीं करना पड़ता। यह अपने block के चलने पर बनता और block के खत्म होने पर चला जाता है।

C Language
void demo() {
    int x = 5;          // yah default se auto hai
    // 'auto int x = 5;' ka matlab bilkul wahi hai
}                        // x yahan gayab ho jata hai

register: गति का संकेत

register compiler से कहता है कि हो सके तो variable को तेज़ CPU register में रखे — बहुत इस्तेमाल होने वाले loop counter के लिए उपयोगी। यह सिर्फ़ एक संकेत है; आधुनिक compilers आमतौर पर खुद बेहतर तय करते हैं।

C Language
for (register int i = 0; i < 1000; i++) {
    // i speed ke liye CPU register mein rakha ja sakta hai
}
💡 इसका address नहीं ले सकते

चूँकि register variable सामान्य memory में न हो, आप उस पर & इस्तेमाल नहीं कर सकते।

static: याद रखने वाली memory

static local variable केवल एक बार बनता है और calls के बीच अपनी value रखता है — यह गिनने के लिए बढ़िया कि function कितनी बार चला।

C Language
#include <stdio.h>
void counter() {
    static int count = 0;   // ek baar bana, yaad rakha
    count++;
    printf("Called %d times\n", count);
}
int main() {
    counter(); counter(); counter();
    return 0;
}
Output:
Called 1 times
Called 2 times
Called 3 times

static के बिना, count हर call में 0 पर reset होकर हमेशा 1 print करता।

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

extern कई source files को एक ही global variable इस्तेमाल करने देता है। आप इसे एक बार define करते हैं, और जहाँ भी चाहिए वहाँ extern से declare करते हैं।

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

// file2.c
extern int total;         // "yah doosri file mein hai"
// ab file2 total istemaal kar sakti hai

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

ClassScopeLifetimeआम उपयोग
autoLocal blockBlock खत्म होने तकसामान्य local variables
registerLocal blockBlock खत्म होने तकतेज़ loop counters (संकेत)
staticLocal या fileपूरा programState याद रखना; file के globals छिपाना
externसभी filesपूरा programGlobal को files में साझा करना

आम गलतियाँ

  • सामान्य local से value याद रखने की उम्मीद करना — आपको static चाहिए।
  • register variable पर & इस्तेमाल करना (अनुमति नहीं)।
  • extern variable दो files में define करना — एक बार define करें, बाकी में declare।
  • static की दो भूमिकाएँ मिलाना: state याद रखना (local) बनाम छिपाना (file स्तर)।
🏋️ अभ्यास

एक function लिखें जिसमें static counter हो जो बताए वह कितनी बार चला, और उसे loop में पाँच बार call करें। फिर static हटाएँ और output को सब 1 में बदलते देखें।

सारांश

  • Storage classes variable का scope और lifetime तय करते हैं।
  • auto local variables का default है (block scope, छोटा जीवन)।
  • register variable को तेज़ register में रखने का संकेत देता है।
  • static calls के बीच value याद रखता है, या file के globals छिपाता है।
  • extern एक global variable को कई files में साझा करता है।

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

C में storage class क्या है?
Storage class compiler को variable के बारे में दो बातें बताता है: उसका scope (program में कहाँ इस्तेमाल हो सकता है) और उसका lifetime (कितनी देर memory में value रखता है)। C में चार हैं — auto, register, static और extern — हर एक इन्हें अलग तरह तय करता है।
Scope और lifetime में क्या अंतर है?
Scope यह है कि variable कहाँ दिखता है — जैसे केवल उस function के अंदर जो उसे declare करता है। Lifetime यह है कि वह memory में कितनी देर रहता है — जैसे function लौटने तक, या पूरे program चलने तक। Storage classes दोनों को नियंत्रित करते हैं।
C में static keyword क्या करता है?
Function के अंदर, static local variable को हर बार नया बनने के बजाय calls के बीच अपनी value रखने देता है, तो वह call count जैसी state याद रख सकता है। File स्तर पर, static variable या function को उसी एक file तक सीमित करता है, दूसरों से छिपाते हुए।
C में default storage class क्या है?
Local variables default रूप से auto होते हैं, यानी वे block चलने पर बनते और खत्म होने पर नष्ट होते हैं, और केवल उसी block के अंदर दिखते हैं। आप auto keyword लगभग कभी नहीं लिखते क्योंकि यह पहले से default है।
extern storage class किसलिए इस्तेमाल होता है?
extern घोषित करता है कि variable किसी दूसरी file में define है, कई source files को एक global variable साझा करने देते हुए। आप variable एक बार बिना extern के define करते हैं, और उसी variable को संदर्भित करने को दूसरी files में extern इस्तेमाल करते हैं।
← 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.