Storage Classes in C Programming in Hindi

इस tutorial में हम बात करेंगे storage classes के बारे में और सीखेंगे की कैसे आप C programming में variables के साथ auto, register, extern और static storage classes use कर सकते हो.

C programming में सभी variables के साथ दो properties associated (जुड़ी) होती है और वो हैं data type और storage class.

Varaible data type ये बताता है की variable किस type की value को store करेगा और variable memory में कितना space occupied करेगा.

Variable storage class ये बताती है की variable को किस memory में create किया जाए, variable की initial value क्या होगी, variable के life कब तक होगी और variable का scope (access level) क्या होगा.

C Language में 4 storage classes हैं auto, register, extern और static. इन सभी storage classes में से आपको सबसे ज्यादा अच्छे तरीके से static storage class के बारे में सीखना है.

Auto Storage Class or Auto Variable in C

वो variables जो हम किसी भी functions (block) के अंदर declare करते हैं उन्हें हम local variable कहते हैं.

अगर आप local variables declaration के वक्त किसी भी stroage class का use नहीं करते हो तो उन variables को auto variables कहा जाता है.

मेरे कहने का ये मतलब है की अगर आप local variable को auto variable बनाना चाहते हो तो आपको variable declaration के वक्त उसके data type से पहले “auto” keyword use करना जरूरी नहीं है.

Auto variable declaration syntax:

data_type variable_name;

//or

auto data_type variable_name;

जब आप auto variable declare करते हैं और उसे कोई value initialize नहीं करते हैं तो compiler उस variable को कोई भी garbage value assign कर देता है.

Auto variables का scope (access level) सिर्फ उसी function के अंदर होता है जिसके अंदर उस auto variables को declare किया गया है.

यानी आप auto variables को उस function के block { } के बाहर use नहीं कर सकते हो जिस function में उसे declare किया गया है.

Auto or Local Variable Example Program:

void fun1()
{
    int x=20;
    printf("%d",x);
}

void fun2()
{
    int y=30;
    printf("%d",y);
    printf("%d",x);    //show error
}

Explanation:

ऊपर program में fun2() function में जब variable x को print कराएँगे तब ये error show की “x is undeclared” क्योंकि variable x हमने fun1() function में declared किया है.

इसलिए उसका जो access level है वो सिर्फ fun1() function के अंदर तक ही है और इसलिए आप variable x को इस function के बाहर कहीं use नहीं कर सकते हो.

अब हम अगर बात करें auto variable की life के बारे में तो मैं आपको बता दूँ की auto variable की life function call से शुरू होती है और जैसे ही function complete हो जाता है उसके साथ ही auto variable की life खत्म हो जाती है यानी उसे भी memory से हटा दिया जाता है.

Register Storage Class or Register Variable in C

जब आप किसी function के अंदर के local variable को declare करते वक्त उसके data type से पहले storeage class register का use करते हैं तो वो variable register variable बन जाता है.

Register variables लगभग local variables की तरह ही होते है बस फर्क ये है की compiler register variable को RAM की जगह computer के microprocessor की register memory में store करता है.

इसका फायदा ये होता है की register variables को access करना auto variables से faster होता है और इससे program का execution time कम हो जाता है.

लेकिन सबसे जरूरी बात Computer की register memory का size बहुत काम होता है इसलिए C programs के हर register variables को उस memory में space नहीं दिया जाता है.

Register variable declaration syntax:

register data_type variable_name;

Register variables का scope भी auto variables की तरह local होता है यानी register variables को उसी function के अंदर होता है जिसके अंदर उस register variables को declare किया गया है.

इसके अलावा एक बात और की आप pointers की help से register variables address नहीं निकाल सकते हो.

Extern Storage Class or Extern Variable in C

वो variables जो हम किसी भी functions के बाहर declare करते हैं उन्हें हम global variables कहते हैं. 

अगर आप global variable declare करते वक्त उसे initialize नहीं करते हैं तो compiler उस variable को value 0 (zero) assign कर देता है.

Global variables का सबसे बड़ा फायदा ये है की आप उन्हें अपने program के किसी भी function के अंदर use कर सकते हो.

अगर आप अपने program के gloabl variables को किसी दूसरे program में use करना चाहते हो तो उसके लिए हम extern storage class का use करते हैं.

जब हम किसी दूसरे program के global variables को अपने program में use करते हैं तब हम अपने program में उस global variable को declare करते वक्त data type से पहले extern storage class का use करते हैं.

जब आप extern variable declare करते हो तब compiler को ये बताते हैं की ये variable किसी और program में declared है और इस program में हम इसे सिर्फ use कर रहे हैं.

इसे हम ऐसे भी कह सकते हैं की जब हम extern variable declare करते हैं तब compiler ये बताते हैं की इस variable को value कहीं और initialized की गयी है और इस program में उस value को हम use या change कर सकते हैं.

इस बात का विशेष ध्यान रखिएगा की जब आप अपने किसी program में extern variable को use करने के लिए उसे declared करें तो उस variable को कोई value initialize ना करें.

Extern storage class example program:

file1.c

#include <stdio.h>
void myfun();

int num=10;     //global variable

int main()
{
    myfun();

    return 0;
}

void myfun()
{
    printf("%d",n);
}

file2.c

#include <stdio.h>

extern int num;     //extern global variable

int main()
{
    printf("%d",n);

    return 0;
}

Output:

10

Explanation:

ऊपर program में file1.c और file2.c दोनों ही एक ही project की 2 files हैं.

ऊपर file2.c में जब हमने global variable num को extern storage के साथ declared किया तो compiler समझ जाएगा की ये variable project की किसी और file में defined है और इसे हम इस program में use कर सकते हैं.

Static Storage Class or Static Variable in C

आप जितनी बार भी किसी function को call करते हो उतनी बार उसमें मौजूद सभी auto variables और register variables और उनकी values को memory में space दिया जाता है. 

उसके बाद जैसे ही function का काम पूरा हो जाता है यानी function call complete हो जाती है उसमें मौजूद सभी variables और उनकी values को memory से हटा दिया जाता है.

लेकिन कभी-कभी हमें अपने programs में ऐसे variables की जरुरत होती है जिनकी life किसी function से ना जुड़ी हो और इस तरह के काम के लिए हम static variables का use करते हैं. 

Static variables की life हमारे program से जुड़ी होती है यानी जब तक program खत्म नहीं होगा तब static variables और उनकी values भी destroy नहीं होगी.

जब हम अपने program में किसी भी variable को declare करते वक्त उसके data type से पहले static storage class का use करते हैं तो वो variable static variable बन जाता है.

अगर आप किसी local variable को static variable बनाएंगे तो आप उसे सिर्फ उसी function (block) में use कर पाएंगे जिसमें आपने static variable declared किया है.

अगर आप किसी global variable को static variable बनाएंगे तो आप उसे अपने program के सभी functions में use कर पाएंगे.

Static variable declaration syntax:

static data_type variable_name;

Normal variable example program:

#include <stdio.h>
void myfun();
int main()
{
    myfun();
    myfun();
    myfun();

    return 0;
}

void myfun()
{
    int i=10;
    printf("%d\n",i);
    i++;
}

Output:

10
10
10

Explanation:

ऊपर program में myfun() function के अंदर variable i एक normal variable है यानी auto variable है. जिसकी life function call से शुरू होती है और function complete हो जाने पर खत्म हो जाती है.

जैसे जब पहली बार myfun() function call होगा तब function के साथ-साथ variable i भी memory में value 10 के साथ initialize किया जाएगा.

function में हमने variable i को print करा कर 1 value से increment किया है यानी variable i की value 10 से 11 हो जाएगी लेकिन इसके तुरंत बाद function complete हो गया.

Function complete होते ही function के साथ variable i और उसकी value भी memory से हटा दिए जाएंगे और program का control वापस वहीँ चला जाएगा जहाँ से उसे call किया गया था.

इसके बाद अब दूसरी बाद myfun() function call होगा और एक फिर नए तरीके से function के साथ-साथ variable i भी memory में value 10 के साथ initialize किया जाएगा.

ये function भी complete हो जाने के बाद पहली function call की तरह memory से हट जाएगा और साथ ही इसके variable भी.

अगर आप चाहते हो की function complete हो जाने के बाद function तो memory से हट जाए लेकिन variable और उसकी value memory से ना हटे तो इसके लिए हम static variable का use करते हैं.

नीचे program में हमने myfun() function के अंदर static variable का use किया है जिसकी वजह से program के output पर क्या फर्क पड़ा वो आप नीचे देख सकते हो.

Static variable example program:

#include <stdio.h>
void myfun();
int main()
{
    myfun();
    myfun();
    myfun();

    return 0;
}

void myfun()
{
    static int i=10;
    printf("%d\n",i);
    i++;
}

Output:

10
11
12

What’s Next: इस tutorial में हमने C Storage Classes के बारे में पढ़ा. Next tutorial में हम C programming में Strings का use करना सीखेंगे