Local scope और global scope में difference को समझने के लिए सबसे पहले आपको C scope rules को समझना पड़ेगा तभी आप आसानी से local scope और global scope को समझ सकते हैं ।
इस tutorial में हम C programming में local scope और global scope के बीच अंतर को समझेंगे साथ ही साथ local variable और global variable किसे कहते हैं वो सब समझेंगे वो भी साधारण हिन्दी भाषा में ।
Local scope और global scope में अंतर | Difference between local variable scope and global variable scope
सबसे पहले scope को थोड़ा समझ लेते हैं की scope होता क्या हैं ?
Scope एक program का एक area होता हैं और variable का दायरा (region) program के क्षेत्र को दर्शाता है जहां variable को declare करने के बाद variable का उपयोग किया जा सकता है।
साधारण हिन्दी भाषा में समझे तो scope को Hindi में दायरा कहते हैं । मतलब scope एक दायरा होता हैं जिसमे variable को declare करने पर वह local variable हैं यदि function के अंदर declare किया गाय हैं या global variable हैं यदि function के बाहर declare किया गया हैं ।
जहां पर variable को declare किया हैं और जहां तक variable को उपयोग किया जा सकता हैं उसे scope कहते हैं ।
C programming में scope 2 तरह के होते हैं ।
1. Local scope (Local variable scope)
2. Global Scope (Global variable scope)
Local scope (Local variable) in C programming -
local scope में variables को function के अंदर declare किया जाता हैं और जिस function में variables को declare किया गया उसी function में उन variables को उपयोग किया जा सकता हैं ।
Function के बाहर या दूसरे function में उन variables का उपयोग नही किया जा सकता हैं क्योकि ऐसा करने से variable undeclare की error मिलती हैं ।
साधारण हिन्दी भाषा से समझे तो local scope variables को access करने की लिमिट होती हैं की यदि variable function block में declare किया गया हैं तो वह उसी function block में access किया जा सकता हैं । तो यह एक local scope होता हैं और जो variables function के अंदर declare किया गए हैं उसी function के ब्लॉक में locally उपयोग किए जाता हैं उन variables को local variables कहते हैं ।
C Local variable को हम पिछले tutorial में भी समझ चुके हैं ।
चलिये C programming में local scope को examples से समझते हैं ।
Examples of local scope in C programming
#include <stdio.h>
int main() {
// local variable declaration
int a,b; // Local scope start
a=12;
b=5;
if(a>b){
printf("a is greater than b ");
}
else
{
printf("b is greater than a ");
}
return 0;
//Local scope end
}
Output –
a is greater than b
उपर दिये गए उदाहरण में हमने variables को main() function में declare किया और main() function में ही उपयोग किया इसलिए ये local variables हैं और आप example में local scope कहाँ से शुरू हैं और कहाँ पर खतम हैं जिसमे हम variables को access कर सकते हैं इसके बाहर local variables को access नही किया जा सकता क्योकि फिर undefined variables की error मिलती हैं जो की नीचे दिये गए उदाहरण में देख सकते हैं ।
#include <stdio.h>
int main() {
// local variable declaration
int a,b;// Local scope start
a=12;
b=5;
func();
//Local scope end
}
int func() {
if(a>b){
printf("a is greater than b ");
}
else
{
printf("b is greater than a ");
}
return 0;
}
Output –
Error: undeclared a and b ;
इस तरह से आप उपर दिये गए C program से समझ सकते हैं की हमने variables को main() function में declare किया और अन्य func() नामक function में variables को access करना चाहा पर ऐसा नही हुआ क्योकि variables local scope में declare किए गए हैं जो की दूसरे फंकशन में access नही किए जा सकते हैं ।
सभी functions में variables को access करने के लिए हम variables को function बाहर डिक्लैर करते हैं जिसके बाद किसी भी फंकशन में या function के बाहर variables को access किया जा सकता हैं जो की global scope में आता हैं और इस तरह के variables को global variables के नाम से जाना जाता हैं ।
Global Scope (Global Variable ) In C programming -
Global scope के नाम से ही पता चल रहा हैं की यदि इस scope में variables को declare किया जाए तो variables को functions के अंदर या बाहर कही भी उपयोग किया जा सकता हैं ।
Global scope में variables को function के बाहर declare या define किया जाता हैं । इन declare हुए variables को global variables कहा जाता हैं जिन्हे किसी भी फंकशन में कितनी भी बार access किया जा सकता हैं। चलिये C programming में global scope को example से समझते हैं ।
Examples of Global scope in C programming | Hindi
#include <stdio.h>
// local variable declaration
int a,b;
int main() {
// local variable declaration
a=12;
b=5;
printf("value of a %d\n",a);
printf("value of b %d\n",b);
func();
}
int func() {
if(a>b){
printf("a is greater than b ");
}
else
{
printf("b is greater than a ");
}
return 0;
}
Output –
value of a 12
value of b 5
a is greater than b
इस उदाहरण में हमने a और b variables को function के बाहर declare किया हैं जो की global scope मे आता हैं अब variables को हमने main() function में भी access किया और func() function में भी access और हमे कोई भी error प्राप्त नही हुई हैं इसका मतलब यहाँ हैं की global scope में variables declare करने पर variables को program में किसी भी जगह function के बाहर या अंदर access किया जा सकता हैं । function के बाहर declare किए गए variables को global variables कहते हैं । अब आप local variable और global variable में अंतर को समझ चुके हैं ।
इस तरह से आपने C programming में local scope vs global scope को समझा या कह सकते हैं local scope और global scope के अंतर (difference) को जाना ।
Recommended Posts:-