Artikel Terbaru

Storage Classes

Introduction to C Storage Classes


Introduction to C Storage Classes 
एक storage class variables और functions का scope और lifetime define करती है। Basically ये एक keyword होता है जो की variable या function के declaration से पहले यूज़ किया जाता है। ये keyword उस variable या function के काम करने के तरीके को बदल देता है। 

C language आपको 4 storage classes provide करती है।

  1. auto storage class 
  2. register storage class 
  3. static storage class 
  4. extern storage class 











C Auto Storage Class 

C की auto storage class सभी local variables के लिए default class मानी जाती है। जब भी आप कोई local variable create करते है और उसके साथ कोई दूसरी storage class define नहीं करते है तो वह by default (automatically) auto storage class को belong करता है।   

इस तरह के variables को automatic variables भी कहा जाता है। इन्हें दर्शाने के लिए आप चाहे तो auto keyword भी यूज़ कर सकते है। आप auto keyword को सिर्फ किसी functions के अंदर ही यूज़ कर सकते है। जब भी function call होता है तो ये variables create होते है और function के exit होने के साथ ही ये destroy हो जाते है। 

int myFunction()
{
     int a; 
     auto int a;
}
  
ऊपर define किये गए दोनों variables ही auto storage class को belong करते है।

C Register Storage Class 

Register storage class ऐसे variables declare करने के लिए यूज़ की जाती है जिन्हें आप RAM की जगह register में store करवाना चाहते है। अकसर ऐसा fast data access के लिए किया जाता है।



एक register computer processor का part होता है जो की small data को hold करता है। लेकिन ये बहुत छोटी value को ही hold कर सकता है। ये RAM से कई गुना तेज होता है।



इसलिए अपने program में आपको जँहा भी तेज data access की requirement हो तो आप इस storage class को यूज़ कर सकते है।



एक बात आपको ये भी ध्यान रखनी चाहिए की जरुरी नहीं की variable को register define करने से ही वो register में save हो जायेगा। ये आपके hardware configuration पर depend करता है।



register int age;

C Static Storage Class

Static storage class compiler को बताती है की variable program के end तक consistent रहेगा। यँहा पर consistent से मेरा मतलब की variable बार बार create और destroy नहीं होगा। 

मान लीजिये आपने function के अंदर एक variable create किया है। जब भी आप इस function को call करते है तो ये variable create होता है और function के end होने के साथ ही ये destroy हो जाता है। 

यानि यदि आप function को call करके उस variable में कोई changes करते है तो function exit होने के बाद वो changes भी नहीं रहेंगे। जब आप दुबारा function को call करेंगे तो variable उसकी initial value के साथ create होगा।     

लेकिन यदि आप static keyword को यूज़ करते है तो variable पुरे program में सिर्फ एक ही बार create होगा और program के end में destroy होगा। और जितनी भी बार आप variable की value change करेंगे वो changes भी पुरे program के दौरान रहेंगे।

#include <stdio.h>

void numFunction();

int main()
{
    numFunction();
    numFunction(); 
    numFunction();
    numFunction();
}

void numFunction
{
    static int num=0;
    num=num+2;
    printf("%d\t",num);
}
   
ऊपर दिए गए program में इस बात से कोई फर्क नहीं पड़ता की आप numFunction() को कितनी बार call कर रहे है। जब भी आप इसे call करेंगे ये function पुराने function call के द्वारा change की गयी num variable की value को ही यूज़ करेगा।



2   4   6   8


C Extern Storage Class 

कई बार ऐसा होता है की आप किसी बड़े project पर काम कर रहे हो तो आपको एक से अधिक program files को handle करना पड़ता है। ऐसी situation में extern storage class बहुत ही helpful होती है।   

मान लीजिये आपने एक program file में एक variable declare किया है। ये variable एक global variable है। अब आप इस variable को इसी project की किसी दूसरी program file में यूज़ करना चाहते है तो आप extern keyword का यूज़ करते है। 

Basically extern keyword compiler को बताता है की ये variable पहले ही किसी class में create किया जा चूका है और आप इसे इस program file में यूज़ करने वाले है। ये keyword पहले से create किये गए किसी variable या function का reference देता है। 
         
File 1

#include <stdio.h>

int num = 5;

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



    return 0;
}
File 2

#include <stdio.h>

extern int num;

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



   return 0;

ऊपर दिए गए उदाहरण में file 2 में compiler को extern keyword द्वारा ये बताया जा रहा है की इस file num variable को यूज़ किया गया है जो की पहले ही दूसरी file में declare हो चूका है।

Post a Comment

0 Comments