Static Storage Class in Hindi
/ BCA / Programming with C and CPP
Static Storage Class in Hindi
What is Static Storage Class in Hindi
C या C++ प्रोग्रामिंग भाषा में **Storage Class** यह तय करता है कि किसी **variable** का जीवनकाल (lifetime), **visibility** (यानि वह कहाँ-कहाँ से access किया जा सकता है), और memory में उसका स्थान किस प्रकार रहेगा। Static Storage Class एक ऐसा विशेष प्रकार का storage class है जो किसी **variable** को स्थायी (permanent) रूप से memory में स्टोर करता है, यानि जब भी static variable को बनाया जाता है, वह पूरी प्रोग्राम के दौरान memory में बना रहता है।
जब हम किसी variable को static बना देते हैं, तो वह एक बार initialize होता है और उसका मान (value) function के बार-बार call होने के बावजूद भी retain (बना रहता है)। यह बहुत उपयोगी होता है जब हमें किसी function में count करना हो कि वह कितनी बार call हुआ है।
Static Variable Scope and Lifetime in Hindi
Static variable का सबसे खास behavior यह होता है कि उसका **lifetime पूरी प्रोग्राम के दौरान** होता है लेकिन उसका **scope उस block या function के अंदर** ही सीमित रहता है जहाँ वह define किया गया होता है।
- जब static variable किसी function में बनाया जाता है, तो वह पहली बार function call पर initialize होता है और फिर हर बार function call पर अपनी पिछली value retain करता है।
- Static variable सिर्फ एक बार memory में allocate होता है, यानि यह बार-बार नया memory block नहीं लेता।
- Static variable का scope local ही होता है, लेकिन उसका lifetime पूरे program तक रहता है।
Static Variable Example in Hindi
नीचे एक सरल example दिया गया है जहाँ static variable की विशेषता को दिखाया गया है:
#include <stdio.h>
void counterFunction() {
static int count = 0; // static variable
count++;
printf("Function called %d times\n", count);
}
int main() {
counterFunction();
counterFunction();
counterFunction();
return 0;
}
ऊपर के प्रोग्राम में जब भी counterFunction() को call किया जाता है, static variable count अपनी value को याद रखता है और हर बार 1 से बढ़ता है। यदि यह static ना होता तो हर बार यह 0 से शुरू होता।
Static in Functions and Blocks in Hindi
Static variables को हम दो जगह इस्तेमाल करते हैं:
- Functions के अंदर: Function के अंदर static variable define करने से वह सिर्फ पहली बार initialize होता है और अगले function calls में previous value retain करता है।
- Global Scope में: जब हम किसी global variable को static बनाते हैं, तो वह केवल उसी file में accessible होता है, मतलब उसे हम अन्य files से access नहीं कर सकते।
Global Static Variable in Hindi
Global variable यदि static keyword के साथ declare किया जाता है, तो वह पूरे प्रोग्राम में accessible नहीं रहता, बल्कि केवल उसी file तक सीमित रहता है जिसमें वह लिखा गया है। इसे file scope कहा जाता है।
// file1.c
static int number = 100; // केवल इसी file में accessible
void display() {
printf("%d", number);
}
ऊपर के कोड में number केवल file1.c में ही accessible है। यदि हम इसे किसी अन्य file में use करना चाहें तो error मिलेगा।
Difference between Static and Automatic Variable in Hindi
Static और Automatic variables में मुख्य अंतर निम्नलिखित हैं:
| Feature | Static Variable | Automatic Variable |
|---|---|---|
| Lifetime | पूरे प्रोग्राम के दौरान | सिर्फ block/function के दौरान |
| Initialization | केवल एक बार | हर बार function call पर |
| Scope | Block/function के अंदर | Block/function के अंदर |
| Default Value | Zero (0) | Garbage (अनिश्चित) |
Static Function in Hindi
C में हम किसी function को भी static बना सकते हैं। इसका मतलब होता है कि वह function केवल उसी file के अंदर accessible रहेगा जहाँ वह define किया गया है।
static void showMessage() {
printf("Hello, I am static function.\n");
}
ऊपर के function को यदि हम किसी दूसरी file में use करना चाहें, तो error मिलेगा क्योंकि यह सिर्फ current file में सीमित रहेगा।
Static Variables in Loop in Hindi
Static variable को अगर किसी loop के अंदर define किया जाए, तब भी वह सिर्फ एक बार initialize होता है। हर बार loop के चलने पर उसकी value retain रहती है।
#include <stdio.h>
int main() {
for(int i = 0; i < 3; i++) {
static int x = 1;
printf("x = %d\n", x);
x++;
}
return 0;
}
इस code का output होगा:
x = 1 x = 2 x = 3
अगर हम static ना लगाते, तो हर बार x = 1 ही print होता क्योंकि हर बार x को दुबारा initialize किया जाता।
Important Points about Static Storage Class in Hindi
- Static variable एक बार initialize होता है और अपनी value retain करता है।
- Static variable का scope उस block या function तक सीमित होता है जिसमें वह define हुआ है।
- Static variable का lifetime पूरे प्रोग्राम तक रहता है।
- Static function केवल उसी file में access किया जा सकता है जहाँ वह लिखा गया है।
- Global static variable केवल उसी source file में accessible होता है।
Where to Use Static Variable in Hindi
- जब किसी function को track करना हो कि वह कितनी बार call हुआ।
- जब किसी data को retain करना हो function calls के बीच।
- Global variable को file-level पर restrict करना हो।
- Encapsulation बढ़ाने के लिए ताकि data leakage ना हो।
Static Storage Class का उपयोग क्यों करें?
- Memory efficiency के लिए – बार-बार allocate/deallocate से बचाव।
- Data persistency के लिए – function के बाहर भी data को save करना।
- Access Control के लिए – data को limited scope में रखना।
Interview में पूछे जाने वाले Static Storage Class से जुड़े प्रश्न:
- Static variable का scope और lifetime क्या होता है?
- Static और Auto variables में क्या अंतर है?
- Global static variable किसे कहते हैं?
- Static function क्या होता है और इसका उपयोग क्या है?
- Static variable और static function के practical example बताइए।