Local Variable in C programming | Local scope | Hindi


पिछले tutorial में हमने C programming में C scope rules के बारे में सीखा था local variable भी उसी में ही आता हैं । 
Scope एक दायरा होता हैं जिसमे variables को declare करने पर उसी दायरे में उन variables का उयोग कर सकते हैं । यदि आपने C scope rules के बारे में नही पढ़ा हैं तो पहले पढ़ले तभी आप आसानी से local variable को समझ पाएंगे ।  
इस tutorial में हम C programming में local variable के बारे में समझेंगे । 

Local variable | Local Scope in C programming | Hindi 

 Local variable को local variable scope के नाम से भी जाना जाता हैं । local variable में , variables जो function block के अंदर declare किए जाते हैं और केवल फंक्शन के अंदर ही उपयोग किए जा सकते हैं, local variables कहलाते हैं । इसे ही local scope के नाम से भी जाना जाता हैं की यदि variables को function block के अंदर declare किया जाए तो variables को उसी function block में उपयोग किया जा सकता हैं । हर एक function का एक ब्लॉक होता हैं यह सब हमने C functions में सीखा था ।  यदि एक function block में variables को declare किया जाए और अन्य function में उपयोग करने की कोशिश  करे तो variables undefined error में मिलता हैं । 
चलिये उदाहरण से C programming में local variable को समझते हैं । 

Local Variable in the C program – 

#include <stdio.h>
int main ()
{
    //local variable declaration 
    int a,b,c;
    //variable initialization
    a = 12;
    b = 22;
    c = a + b;
    printf ("Sum of a and b= %d\n", c);
    return 0;
}

Output – 

Sum of a and b= 34

उपर दिये गए उदाहरण में आप देख सकते हैं की हमने main() function के अंदर ही variable को declare करके उपयोग किया जो की function के ब्लॉक में ही हैं । 

यदि variables को एक function में declare करके अन्य function में उपयोग करे तो तब वहाँ पर error मिलती हैं जो की नीचे दिये गए उदाहरण में देख सकते हैं । 

#include <stdio.h>
int main ()
{
    //local variable declaration 
    int a,b,c;
    //variable initialization
    a = 12;
    b = 22;
    additon() ; 
}
int additon() 
{
     c = a + b;
    printf ("Sum of a and b= %d\n", c);
    return 0;
}

Output – 

Error: Undefined or undeclared  a b c ; 

इस उदाहरण में हमने एक user defined function addition() बनाया हैं यदि आपको functions बनाने के बारे में नही पता तो पहले C functions के बारे में अच्छे से समझले । इस उदाहरण में हमने main() function में variables को declare किया और addition() function में उन variables को उपयोग करना चाहा पर हमे  यहाँ पर variables undefined की error प्राप्त होती हैं । क्योकि यदि variables एक फंकशन के अंदर declare या define किए जाए तो अन्य function में उन variables को access नही किया जा सकता हैं क्यूकी ये लोकल scope होता हैं और variable को locally किसी ब्लॉक में declare करने के बाद उसी function के ब्लॉक में उपयोग करना पड़ता हैं अन्यथा error मिलती हैं । 
यदि हमे इन variables को program में किसी भी function में access करना हैं तो उसके लिए variables को सभी functions के बाहर declare करना पड़ता हैं । जो की global variable में होता हैं जिसे हम global scope कहते हैं । 
चलिये उदाहरण से समझते हैं – 

#include <stdio.h>
  int a,b,c;    //Global variable declaration 

int main ()
{
    //variable initialization
    a = 12;
    b = 22;
    additon() ; 
}
int additon() 
{
     c = a + b;
    printf ("Sum of a and b= %d\n", c);
    return 0;
}

Output – 

Sum of a and b= 34

उपर दिये गए उदाहरण में हमने variables को globally declare किया जैसा की आप उपर दिये गए उदाहरण में देख सकते हैं की हमने variables को  किसी functions के अंदर declare  नही किया हैं variables हमने functions के बाहर declare किए हैं । 
इसी वजह से कोई भी error नही मिली हैं । अब इन variables को पूरे program में कही भी उपयोग किया जा सकता हैं । 
Global variable को ओर अधिक यहाँ पर जाने – global variable in C 
 अब तो आप समझ ही गए होंगे की local variable में function block में variable को declare किया जाता हैं तथा जिस function block में declare किया हैं उसी में ही उपयोग भी किया जाता हैं । इसे ही हम local variable कहते है । 
इस तरह से C programming language में local variable का उपयोग किया जाता हैं । 


Please Share

Recommended Posts:-


Live Chat

Hello! How can we assist you today?