Notes in Hindi

Extern Storage Class in Hindi

/ BCA / Programming with C and CPP

Extern Storage Class in C Explained

Extern Storage Class in Hindi

What is Extern Storage Class?

जब हम C या C++ जैसी programming language में एक से ज्यादा files का उपयोग करते हैं, तब हमें कई बार ऐसे variables या functions की जरूरत होती है जो एक file में define हों लेकिन दूसरी file से भी access किए जा सकें। ऐसे में extern storage class का प्रयोग किया जाता है।

Extern एक ऐसा keyword होता है जो यह बताता है कि किसी variable या function की memory कहीं और पहले से ही allocate हो चुकी है। इसका मतलब यह होता है कि हम उस variable या function को यहां सिर्फ use कर रहे हैं, define नहीं कर रहे।

Basic Syntax of Extern

extern int count; // यह बताता है कि count नाम का एक int variable कहीं और define किया गया है

Key Points About Extern

  • Extern keyword को use करके हम किसी global variable को एक से अधिक files में access कर सकते हैं।
  • Extern variable को initialize नहीं किया जाता, सिर्फ declare किया जाता है।
  • इसका scope पूरे program में होता है।
  • यह memory को उस स्थान पर allocate करता है जहां वह variable actually define किया गया है।

Understanding Extern Keyword in Hindi

मान लीजिए हमारे पास दो files हैं: main.c और file1.c। अब अगर हम file1.c में कोई global variable बनाते हैं और उसे main.c में use करना चाहते हैं, तो हम extern keyword का इस्तेमाल करेंगे।

Example: Using Extern Across Files

file1.c:

int count = 10; // variable की definition

main.c:

#include <stdio.h> extern int count; // variable की declaration int main() { printf("Value of count is %d", count); return 0; }

इस program में main.c file में count नाम के variable को केवल declare किया गया है extern keyword की मदद से, लेकिन उसकी memory file1.c में allocate की गई है। जब हम इन दोनों files को एक साथ compile करते हैं तो program सही से काम करता है।

How Extern Works Across Files in Hindi

जब हम एक बड़ी C project पर काम करते हैं, तब code को अलग-अलग files में बांटना एक अच्छी practice होती है। इससे code modular, readable और maintainable बनता है। परंतु जब multiple files में variables और functions use करने की जरूरत पड़ती है, तब extern keyword हमारी मदद करता है।

Extern यह सुनिश्चित करता है कि अगर किसी file में कोई variable पहले से define है, तो हम उसे दूसरी file में भी access कर सकते हैं बिना दोबारा memory allocate किए।

Steps of How It Works

  • सबसे पहले किसी एक file में global variable define करें।
  • फिर बाकी files में उस variable को extern keyword के साथ declare करें।
  • Compile करते समय सभी files को एक साथ compile करें ताकि linker सभी declarations और definitions को जोड़ सके।

Use Cases of Extern Storage Class in Hindi

1. Global Variables Across Files

  • जब एक variable को एक से ज्यादा files में use करना हो, extern सबसे best तरीका होता है।
  • Global variable को define एक बार करें और extern से बाकी files में access करें।

2. Modular Programming में Use

  • अगर आप large project बना रहे हैं तो code को अलग-अलग modules में divide करना चाहिए।
  • हर module में common variables या configuration variables को extern से manage करना आसान होता है।

3. Function Declaration में भी

  • Functions को भी extern से declare किया जा सकता है। हालांकि C में default function declaration को extern माना जाता है।

4. Header Files में Extern का Use

हम अक्सर header files में extern declarations लिखते हैं ताकि सभी source files उन्हें access कर सकें।

example.h:

extern int configValue;

main.c:

#include "example.h" int main() { printf("%d", configValue); return 0; }

config.c:

int configValue = 42;

इस तरीके से extern का प्रयोग करके हम code को efficiently manage कर सकते हैं।

Differences Between auto, static, extern and register

Storage Class Scope Storage Default Value Usage
auto Local Memory Garbage Local variable (default)
static Local/Global Memory 0 Value retain रहती है
extern Global Memory कहीं और Depends Cross file variable use
register Local CPU Register Garbage Fast access

Important Rules and Tips

  • Extern variable को initialize नहीं किया जाता, केवल declare किया जाता है।
  • Variable या function को केवल एक बार define करें।
  • Multiple files को एक साथ compile करें वरना “undefined reference” error आ सकती है।
  • Extern variable को header file में declare करना best practice है।

Common Errors While Using Extern

  • एक ही variable को एक से ज्यादा बार define कर देना जिससे “multiple definition” error आ जाती है।
  • Variable को declare तो किया extern से लेकिन define नहीं किया – जिससे linker error आता है।
  • Files को एक साथ compile नहीं करना – जिससे variable मिल ही नहीं पाता।

Summary Points (for Revision Purpose)

  • Extern का उपयोग global variables को एक से अधिक files में use करने के लिए किया जाता है।
  • यह memory allocate नहीं करता, बस variable की reference देता है।
  • Code modular और organized बनाने में मदद करता है।
  • Large projects में extern बहुत ही उपयोगी होता है।
  • Always use extern declaration in header files for better practice.

FAQs

Extern storage class का उपयोग किसी ऐसे variable या function को reference करने के लिए किया जाता है जो किसी और file में define किया गया हो। यह memory allocate नहीं करता, सिर्फ बताता है कि यह variable कहीं और already define है।
जब एक file में कोई variable define किया जाता है और दूसरी file में उसी variable को extern से declare किया जाता है, तब दोनों files को एक साथ compile करने पर extern variable को original definition से link कर दिया जाता है। इससे वह variable दूसरी file में भी accessible हो जाता है।
नहीं, extern variable को सिर्फ declare किया जाता है, initialize नहीं। इसका उद्देश्य केवल यह बताना होता है कि variable कहीं और define किया गया है। Initialization केवल उस file में होती है जहाँ variable define होता है।
Extern variables को आमतौर पर header files में declare किया जाता है ताकि multiple source files उन्हें include करके access कर सकें। इससे code organized और manageable रहता है।
Common errors में शामिल हैं: extern variable को define किए बिना use करना जिससे "undefined reference" error आती है, या एक ही variable को कई files में define करना जिससे "multiple definition" error होती है।

Please Give Us Feedback