What is Storage Classes in C in Hindi - C में Storage Classes क्या है?

C में Storage Classes एक Variable के features का वर्णन करता है
Storage Classes, Keywords होते है जो एक Program के अंदर Variables के scope, lifetime और visibility को Define करते है

ये निर्धारित करते है कि Variables कैसे और कहां Memory में Store होते है और Program execution के दौरान वे कैसे व्यवहार करते है।

C कई Storage classes, प्रदान करते है और प्रत्येक का विशेष characteristics होता है

Types of Storage Classes in C in Hindi - C में Storage Classes के प्रकार

C language में निम्न चार Storage classes का Use किया जाता है।
1) auto
2) extern
3) static
4) register

auto

C में `auto` Storage class मूलभूत Storage में से एक है जिसका प्रयोग function के अंदर Variables को Define करने में होता है

जब किसी Variable को auto keyword से Declare किया जाता है, तो उसे अपने आप Memory मिलती है और हर बार Function Call होने पर वह फिर से Initialize होता है।

 `auto` storage class से सम्बंधित कुछ मुख्य बाते


1) C में यदि एक Variable एक function के अंदर बिना storage class के Declare किया जाता है तो इसे स्वचालित रूप से `auto` storage class माना जाता है

2) function call करने पर `auto` storage class वाले Variables को स्वचालित रूप से Stack पर Memory मिल जाती है। 

यदि Variable को सही ढंग से Initialize नहीं किया गया है तो उन्हें Garbage Values के साथ Initialize किया जाता है।

3) 'auto` storage class वाले Variables का उस function के भीतर एक सीमित दायरा होता है जहां उन्हें Declare किया जाता है वे function के बाहर Access नही किए जा सकते हैं

4) 'auto` Variables का जीवनकाल तब शुरू होता है जब function का execution शुरू होता है और function के बाहर निकलने पर समाप्त होता है। 
 
'auto` Variables के लिए allocate memory, function के बाहर निकलने पर स्वचालित रूप से हटा दी जाती है।
Program for auto variable
#include<stdio.h>
int main() 
{
 auto int autoVar = 15;  
// Declaration of an auto variable
    
 printf("autoVar: %d\n", autoVar);

// Changing the value
    autoVar = 25;
printf("After changing value of autoVar:  %d\n", autoVar);

    return 0;
}
 
Output:
autoVar: 15
After changing value of autoVar: 25

Explanation
1) सबसे पहले auroVar नाम से एक auto variable declare किया गया है और उसे 15 value Assign कर Initialize किया गया है

2) यदि हम इस variable के पहले auto keyword Use नही करते तो भी यह default storage classe होने से auto variable ही होता

3) अब `printf' का Use कर `autoVar` की Initial Value जो कि 15 है Print की जायेगी

4) उसके बाद `autoVar` की Value 25 Change कर उसे पुन: Print की जायेगी

extern

extern keyword का प्रयोग global variable या function को Declare करने मे किया जाता है

इसका प्रयोग ऐसे variable या function को Declare करने में किया जाता है जिसे पहले से अन्य file में बनाया गया है।

जब variable को extern keyword का Use करके defined किया जाता है तो उसे global variables कहते है।
इन variables को पूरे Program में Access किए जाते है। 

हमे ध्यान रखना चाहिए कि extern variable को Initialize नही किया जाता क्योंकि यह पहले से ही Defined रहता है

अतः यह हमे ऐसे variables या functions को Access करने का तरीका प्रदान करता है जो बाहर (externally) बनाया गया होता है और जिसे हम पूरे प्रोग्राम में कई files में share और reuse कर सकते हैं।
Program for extern in C
#include <stdio.h> // Declaration of a global variable extern int x;
int main() {     // Here the extern variable x is accessible     printf("Value of x is: %d\n", x);     return 0; }
// Here global variable is defined outside the main function
int x = 5;
Output:
Value of x is : 5

Explanation:
1) `extern int x;`: 
सबसे पहले एक global variable `x` बिना define किए declare किया गया था जो compiler को बताता है कि 'x' variable Program में कही पर define किया गया है

2) अब In `main()`,
में `x` को print करने के द्वारा Access किया जाता है चूकि  `x` variable को `extern`, के रूप में declare किया गया है अतः compiler जानता है कि `x` को वास्ताविक रूप `main()` function के बाहर Define किया गया है

3) `int x = 5;`: 
यह line `main()` function global variable `x` के बाहर में define होने की सूचना देता है
 जो से 'x' की value को वास्तविक रूप memory allocation प्रदान करता है

अतः `extern` कीवर्ड का Use करके, हम complier को 'x' variable की existence के बारे में बताते है और तब `main()`, के बाहर 'x' को define और Initialize करके यह सुनिश्चित करते है कि इसे Program में सही तरीके से Access और Use किया जा सके
 

static

C में static Storage class का उपयोग variables और functions के scope, lifetime और storage location को बदलने के लिए करते हैं।
एक बार जब static variable define हो जाता है तब यह Program के अंत तक रहता है। static variable by default '0' से Initialize होता है और उसका Scope local static variable के अंदर सीमित रहता है

static Storage classes संबंधित मुख्य बातें:


1) Static variables का एक जीवनकाल होता है जो Program के execution के दौरान विस्तारित होता है। Program शुरू होने पर उन्हें एक बार Initialize किया जाता है और प्रोग्राम समाप्त होने तक उनके मान वैसे ही रहते है

2) जब static variable को किसी function के भीतर उपयोग किया जाता है, तो static variable का local scope होता है लेकिन function Call में उनके Value वैसे ही बने रहते हैं। 

static variable केवल उस block या function के भीतर ही Access किए जाते है जहां उन्हें declare किया गया है

3) जब static variable को function के बाहर (global scope) या function के साथ उपयोग किया जाता है, 

तो static keyword, variables या functions की visibility को उस File तक सीमित कर देता है जहां उन्हें defined किया गया है

4) Static variables, Computer के memory के fixed भाग मे store किए जाते है इसका मतलब यह है कि वे पूरे प्रोग्राम के दौरान वहीं रहते हैं

5) यदि हम किसी File में किसी function को static बनाते हैं, तो इसका उपयोग केवल उस File के भीतर ही किया जा सकता है

6) static variables को Access करना उन variables को Access करने की तुलना में तेज़ हो सकता है जो अक्सर बदलते रहते हैं। 

ऐसा इसलिए है क्योंकि कंप्यूटर को ठीक-ठीक पता होता है कि Memory में static variable कहाँ हैं, इसलिए उसे उनकी तलाश में समय बर्बाद करने की आवश्यकता नहीं है

7) यदि हम किसी static variables को बनाते समय उसे कोई मान नहीं देते हैं, तो यह स्वचालित रूप से शून्य पर set हो जाता है 

8) यदि एक static variable को स्पष्ट रूप से Initialize नहीं किया गया है, तो यह स्वचालित रूप से शून्य (0) पर Initialize हो जाता है 
Program for static variable
#include<stdio.h>
//Definition of a global static variable
static int myVar = 5;

void changeVar() 
{
    myVar += 15;
}
int main() 
{
    printf("Value of myVar before change: %d\n", myVar);
    
     changeVar() ;
printf("Value of myVar after change: %d\n", myVar);
    return 0;
}
 
Output:
Value of myVar before update: 5
Value of myVar after update: 20

Explanation:

1) `static int myVar = 5;'
यह statement एक global static variable जिसका नाम `myVar` है Define करता और उसे 5 Value Assign कर Static होने के कारण, इसका मान Program के Execution के दौरान बना रहता है

2) `changeVar()` function के द्वारा `myVar` की Value को Add करने के द्वारा 15 से 20 मे Modify करते है

3) `main()`, में `myVar` शुरुवाती Value को Print करते है तथा `changeVar()` function को Call करके `myVar` की Update की गई Value को Print करते है

संक्षेप में, एक static variable, विभिन्न function call में भी अपना Value बनाए रखता है अर्थात एक function यदि Run करना समाप्त कर रहा है 

तो भी static variable अपना Value याद रखता है जैसे ऊपर के Program में इसने अपनी Value शुरुवाती Value 15 को याद रखा और function call में Updated value 20 किया 

और यह केवल उस File के भीतर ही Accessible होता है जहां इसे Declare किया गया है। इससे जानकारी को याद रखना और एक ही File में Function के बीच डेटा साझा करना आसान हो जाता है।

register

C में register storage class का उपयोग compiler को यह बताने के लिए होता है कि एक विशेष variable को CPU register में store होना चाहिए ताकि उसे तेजी से access किया जा सके 
क्योंकि CPU का register, memory से भी तेज होता है अर्थात CPU के register से किसी भी variable को memory की अपेक्षा जल्दी से एक्सेस किया जा सकता है।

register storage class के बारे मे कुछ महत्वपूर्ण बातें निम्न है


1) `register` keyword का Use variable declarations के पहले यह सूचित करने के लिए होता है कि है यदि संभव है तो variable को एक register में store होना चाहिए 

2) `register` keyword, Compiler के लिए एक request है कि यह variable को एक register में allocate कर सकता है या नहीं भी कर सकता Compiler अनुकूलन मानदंड और उपलब्ध registers के आधार पर निर्णय लेता है

3) चूंकि registers में Memory में store variables तरह memory address नहीं होते हैं, इसलिए आप 'register' के रूप में declare variables के साथ ऑपरेटर के एड्रेस (`&`) का उपयोग नहीं कर सकते हैं।

4) `register` keyword के साथ declare Variables का scope वैसे ही होता है जैसे `auto` storage class के साथ declare किए हुए variables का होता है तथा दोनों का जीवनकाल भी समान होता है

5) `register` keyword कर Use ऐसे variables के लिए अच्छा है जिसे अक्सर Access किया जाता है तथा जिसकी memory address की जरुरत नही है

6) सभी variables को register की भांति declare नही किया जा सकता
उदाहरण के लिए बहुत बड़े variables को या जिसे जटिल operations की जरुरत है ऐसे variables को register storage में रखना उचित नहीं है 
Program for register variable
#include<stdio.h>
int main() 
{
    register int regVar = 25;  // Declaration of a register variable
    
    int *ptr = &regVar;
    
    printf("Value at ptr: %d\n", *ptr); 
    printf("Value of regVar : %d\n", regVar);
    
    return 0;
}
 
Output:
Main.c: In function ‘main’:
Main.c:5:1: error: address of register variable ‘regVar’ requested

Explaination:
1) ऊपर Program में सबसे पहले एक regVar, register variable declare कर उसे 25 Value Assign कर Initialize किया गया है

2) regVar के पहले register keyword यह सूचित करता है कि यह एक register variable है जिसे CPU register में Store होना है

3) int *ptr = &regVar;
उपरोक्त statement यह सूचित करता है कि एक 'ptr ' नाम से Pointer बनाया गया जो कि regVar Variable के Address को point करता है

4) चुकि regVar एक register variable है अतः इसका कोई Memory Address नही है 

5) अतः जैसे हमने इसके Memory Address को print करना चाहा हमे Output में error मिला।

C में सही Storage class का चयन करना क्यों महत्वपूर्ण है?

C में सही Storage class का चयन करना इसलिए महत्वपूर्ण है क्योंकि यह वेरिएबल को कितने समय के लिए संग्रहित(variable lifetime) और उपयोग किया जाएगा इस बात को प्रभावित करता है।

यह मेमोरी को मैनेज करने में मदद करता है और प्रोग्राम को तेजी से चलाता है। उदाहरण के लिए register variable आपको तेजी से एक्सेस करने की सुविधा देता है।

 वैसे ही static variable, function call के बीच में अपने वैल्यू को बनाए रखता है। extern variables, का उपयोग अलग-अलग फाइलों में होता है।  

सही storage class का चुनाव करने से आपका प्रोग्राम बेहतर कार्य करता है और बड़ी बुद्धिमानी से रिसोर्सेस का उपयोग करता है।

Differences between auto and static storage classes in C in Hindi 

1. auto
जब block शुरू होता है तब मेमोरी दिया जाता है और जब block समाप्त हो जाता है तब मेमोरी को ले लिया जाता है।
static
मेमोरी को एक बार दिया जाता है तथा वह पूरे प्रोग्राम में ऐसे ही बना रहता है।

2. auto:
अगर variables को initialize न किया जाए तो उसमें रेंडम वैल्यू रहती हैं।
static: अगर variables को initialize न किया जाए तो उसमें अपने आप zero set हो जाता है।

3. auto
यह block या function के अंदर कार्य करता है जहां उसे  डिक्लेयर किया गया है।
static:
block के अंदर कार्य करता है परंतु function calls के बीच में वैल्यू को बनाए रखता है।

4. auto:
variable तब तक मौजूद रहता है जब तक फंक्शन चल रहा है। 
 static: variable, पूरे प्रोग्राम के लिए मौजूद रहता है।

5. auto:
जब भी ब्लॉक या फंक्शन को कॉल किया जाता है तो वेरिएबल को फिर से initialize किया जाता है।    
static
इसे एक ही बार initialize किया जाता है जब प्रोग्राम शुरू होता है।

6. auto
स्वचालित रूप से इसका उपयोग होता है।
इसे डिक्लेअर करने की जरूरत नहीं है। static
function calls के बीच में वैल्यू को बनाए रखने के लिए इसे अवश्य ही डिक्लेयर किया जाता है। 


 Related Post