What is Scope of Variables in C Language in Hindi
/ BCA / Programming with C and CPP
Scope and Lifetime of Variables in C Language in Hindi
What is Scope of Variables in C Language in Hindi
Introduction
C language में जब हम कोई variable बनाते हैं, तो वह variable किस जगह से access किया जा सकता है, यह "Scope" पर निर्भर करता है। Scope का मतलब होता है कि एक variable किस-किस हिस्से (block/function) में मान्य (valid) है। अगर किसी variable को उसके scope से बाहर access करने की कोशिश की जाए तो compiler error देता है।
Scope of Variable क्या होता है?
Scope का मतलब होता है "सीमा" या "range"। यानी कोई variable कहां से कहां तक काम करेगा। यह इस बात पर निर्भर करता है कि वह variable कहां declare किया गया है – किसी function के अंदर, function के बाहर या किसी block के अंदर।
Types of Scope in C Language
- Local Scope: जो variable किसी block या function के अंदर declare किया जाता है, वह सिर्फ उसी block या function के अंदर valid होता है।
- Global Scope: जो variable main function या किसी भी function के बाहर declare किया जाता है, वह पूरे program में कहीं भी accessible होता है।
- Block Scope: जो variable किसी छोटे block जैसे for, while, if आदि के अंदर declare होता है, वह सिर्फ उसी block में valid होता है।
- Function Scope: C में labels (जैसे goto में use होने वाले labels) का scope सिर्फ function के अंदर होता है।
उदाहरण के साथ समझें
#include <stdio.h>
int a = 10; // global variable
void demo() {
int b = 20; // local variable
printf("%d\n", a); // valid
printf("%d\n", b); // valid
}
int main() {
int c = 30; // local variable
printf("%d\n", a); // valid
printf("%d\n", c); // valid
// printf("%d\n", b); // invalid - b local है demo() के अंदर
return 0;
}
What is Lifetime of Variables in C Language in Hindi
Lifetime क्या होता है?
Lifetime का मतलब होता है कि एक variable memory में कब से कब तक रहता है यानी उसकी memory कब allocate होती है और कब free होती है। इसका मतलब ये नहीं कि वह कहां से access हो सकता है, बल्कि ये कि वो कब memory में मौजूद रहता है।
Types of Lifetime
- Automatic Lifetime: जो variables किसी function या block के अंदर बनाए जाते हैं (local variables), उनका lifetime function के call होने पर शुरू होता है और function के खत्म होते ही खत्म हो जाता है। इन्हें
autovariable भी कहा जाता है। - Static Lifetime: जो variable static keyword से बनाए जाते हैं, उनका lifetime पूरे program के duration तक रहता है। चाहे वो किसी function के अंदर ही क्यों न बने हों।
- Dynamic Lifetime: जो variables heap memory में malloc, calloc जैसी functions से बनाए जाते हैं, उनका lifetime हम खुद control करते हैं। जब तक हम manually free नहीं करते, वो memory में रहते हैं।
- Global Lifetime: global variables का lifetime भी पूरे program के दौरान रहता है, जैसे static variables का।
उदाहरण:
#include <stdio.h>
void test() {
int a = 10; // automatic lifetime
static int b = 20; // static lifetime
printf("a = %d, b = %d\n", a, b);
b++;
}
int main() {
test(); // a हर बार नया बनेगा, b एक बार बनेगा और value retain करेगा
test();
return 0;
}
Block-level vs Function-level Scope in Hindi
Block-level Scope क्या है?
जब हम कोई variable किसी block जैसे for, while, if-else, या curly braces {} के अंदर declare करते हैं, तो वह केवल उस block के अंदर ही valid होता है। Block के बाहर वो variable exist नहीं करता।
Function-level Scope क्या है?
Function-level Scope का मतलब होता है कि एक variable पूरे function के अंदर valid होता है। लेकिन C language में यह सिर्फ labels के लिए valid है, जैसे goto के label। Variables के लिए function-level scope नहीं होता; उनके लिए block-level scope ही लागू होता है।
Block vs Function Scope का अंतर
| मापदंड | Block-level Scope | Function-level Scope |
|---|---|---|
| Definition | Variable block के अंदर ही accessible होता है | Label पूरे function के अंदर accessible होता है |
| Use | Variables (int, char, float आदि) | Labels (goto के लिए) |
| Access Range | केवल block के अंदर | पूरे function में |
उदाहरण:
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
int y = 20; // y का block-level scope है
printf("%d\n", y);
}
// printf("%d\n", y); // invalid - y block के बाहर access नहीं हो सकता
label1:
printf("This is a label\n");
goto label1; // function-level scope of label
return 0;
}
Importance of Scope and Life in memory usage in Hindi
Memory के efficient उपयोग में Scope और Lifetime का महत्व
- जब हम variables को छोटे scopes में declare करते हैं, तो memory allocation कम समय के लिए होता है जिससे memory का उपयोग बेहतर होता है।
- Global और static variables लगातार memory में रहते हैं, जिससे program का memory footprint बढ़ सकता है। इसलिए उनका उपयोग जरूरत पर ही करें।
- Function के अंदर declare किए गए local variables सिर्फ function के execution तक memory में रहते हैं, जिससे system resources की बचत होती है।
- अगर हम किसी variable को केवल एक block में use करने वाले हैं, तो उसे global बनाना memory wastage कहलाएगा।
- Scope का सही उपयोग करने से code readability और maintainability बढ़ती है।
उदाहरण:
#include <stdio.h>
int globalVar = 100; // unnecessary memory usage if not always used
void myFunction() {
int temp = 50; // better scope and lifetime for limited use
printf("Temp = %d\n", temp);
}
int main() {
myFunction();
return 0;
}
समझने की सरल भाषा में
जैसे किसी इंसान का जीवन भी एक समय तक सीमित होता है, वैसे ही variables का भी एक जीवन (lifetime) और काम करने की जगह (scope) होती है। हमें यह देखना होता है कि कौन-सा variable कब तक memory में रहे और कहां-कहां से access हो, जिससे हमारा program ज्यादा memory consume न करे और तेजी से चले।