Storage Classes in C

Introduction

जब हम C language में variables declare करते हैं, तो सिर्फ उनका data store करना ही important नहीं होता, बल्कि यह भी जानना जरूरी होता है कि:

  • variable कहाँ store होगा (memory location)
  • वह कितने समय तक memory में रहेगा (lifetime)
  • उसे program में कहाँ-कहाँ use किया जा सकता है (scope)

इन्हीं सभी चीजों को control करने के लिए C language में Storage Classes का concept दिया गया है।

Simple शब्दों में:
Storage class यह बताता है कि variable का behavior क्या होगा

उदाहरण के लिए:

  • कुछ variables temporary होते हैं (function के अंदर बनते और खत्म हो जाते हैं)
  • कुछ variables पूरे program में रहते हैं
  • कुछ variables अपनी value याद रखते हैं

इन अलग-अलग behaviors को manage करने के लिए Storage Classes use होते हैं।

Definition

Storage Class वह mechanism है जो variable की scope, lifetime और memory location को define करता है।

Types of Storage Classes

C में चार प्रकार के storage classes होते हैं:

  1. Automatic (auto)
  2. External (extern)
  3. Static
  4. Register

1. Automatic Storage Class

Explanation

जब भी हम function के अंदर कोई variable declare करते हैं, तो by default वह automatic storage class में आता है।

इसका मतलब:

  • variable function के start में बनता है
  • function खत्म होते ही destroy हो जाता है

इसलिए इसे temporary variable भी कह सकते हैं।

Key Points

  • Default storage class है
  • Scope → केवल function तक
  • Lifetime → function execution तक
  • Initial value → garbage

Example

#include <stdio.h>
void test()
{
int x = 5;
printf("%d\n", x);
}
int main()
{
test();
return 0;
}

Output:

5

Explanation:

  • x सिर्फ test() function के अंदर exist करता है
  • function खत्म → variable खत्म

2. External Storage Class

Explanation

extern का उपयोग तब किया जाता है जब हमें global variable को दूसरे function या file में use करना हो

इसका मतलब:

  • variable program के बाहर declare हो सकता है
  • लेकिन use कहीं भी किया जा सकता है

Key Points

  • Scope → पूरे program में
  • Lifetime → पूरे program execution तक
  • Default value → 0

Example

#include <stdio.h>
int x = 10; // global variablevoid show()
{
extern int x;
printf("%d\n", x);
}
int main()
{
show();
return 0;
}

Output:

10

Explanation:

  • x global है
  • extern से उसे function में access किया

3. Static Storage Class

Explanation

Static variable खास होता है क्योंकि:
यह अपनी value को याद रखता है (retain करता है)

Normal variable हर बार नया बनता है, लेकिन static variable:

  • एक बार बनता है
  • और हर function call के बाद भी value save रहती है

Key Points

  • Scope → local (अगर function में है)
  • Lifetime → पूरे program तक
  • Default value → 0

Example

#include <stdio.h>
void test()
{
static int x = 0;
x++;
printf("%d\n", x);
}
int main()
{
test();
test();
test();
return 0;
}

Output:

1
2
3

Explanation:

  • x destroy नहीं हुआ
  • हर बार value increase होती गई

4. Register Storage Class

Explanation

Register storage class का उपयोग variable को CPU register में store करने के लिए किया जाता है।

इसका फायदा:

  • variable fast access होता है
  • performance बेहतर होती है

लेकिन:

  • यह compiler पर depend करता है
  • जरूरी नहीं कि हमेशा register में ही store हो

Key Points

  • Scope → local
  • Lifetime → function तक
  • Address (&) नहीं ले सकते

Example

#include <stdio.h>
int main()
{
register int i;
for (i = 1; i <= 5; i++)
{
printf("%d ", i);
}
return 0;
}

Output:

1 2 3 4 5

Explanation:

  • i को fast access के लिए use किया गया

Comparison Table

Storage ClassScopeLifetimeDefault ValueSpecial Feature
autoLocalFunction तकGarbageDefault variable
externGlobalपूरे program तक0Global access
staticLocal/Globalपूरे program तक0Value retain करता है
registerLocalFunction तकGarbageFast access

Technical Understanding

  • auto → temporary variables
  • extern → global sharing
  • static → memory retain करना
  • register → performance optimization

Exam Points

  • Storage class = scope + lifetime define करता है
  • चार types होते हैं
  • static value retain करता है (important)
  • extern global variable access करता है
  • auto default होता है

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top