BCA / B.Tech 12 min read

union in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: C Language Notes in Hindi | C language के नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: union in Hindi - General Topics
  • • अवलोकन: Quick Answer: C language में Union एक user-defined data type है जो विभिन्न प्रकार के data types (int, float, char आदि) को एक ही memory location में stor...
  • • मुख्य अवधारणाएं: Union, Definition memory sharing access Hindi, Members struct vs union,Union, Definition memory sharing access, Members struct vs union
Quick Answer: C language में Union एक user-defined data type है जो विभिन्न प्रकार के data types (int, float, char आदि) को एक ही memory location में store करने की अनुमति देता है। Structure की तुलना में जहाँ हर member को अलग memory मिलती है, Union में सभी members एक ही memory space शेयर करते हैं। Union का कुल size उसके सबसे बड़े member के size के बराबर होता है, और एक समय में केवल एक ही member में मान्य मान सुरक्षित रखा जा सकता है।

C Language में Union क्या है? (What is Union in C in Hindi)

C programming में जब हमें अलग-अलग data types के variables को एक समूह में रखना होता है, तो हम structure या union का use करते हैं। Union की सबसे बड़ी खासियत memory efficiency है। जहाँ structure अपने सभी members के sizes को जोड़कर कुल memory बनाता है, वहीं union केवल अपने सबसे बड़े member के बराबर memory allocate करता है।

अगर आप C भाषा में dynamic memory allocation समझना चाहते हैं, तो देखें: Memory Allocation in C in Hindi और malloc and calloc in C in Hindi। arrays के साथ data grouping सीखने के लिए पढ़ें: What is Array in Hindi।

Advertisement

Union का Syntax और Declaration

Union को declare करने के लिए union keyword का use किया जाता है। इसका सिंटैक्स structure जैसा ही होता है:

union UnionName {
    data_type member1;
    data_type member2;
    data_type member3;
};

Example:

union Record {
    int roll_no;       // 4 bytes
    float percentage;  // 4 bytes
    char grade;        // 1 byte
};

Union में Memory Allocation कैसे काम करता है?

मान लीजिए कि ऊपर दिए गए Record union का एक variable r1 बनाया जाता है:

  • int roll_no: 4 bytes
  • float percentage: 4 bytes
  • char grade: 1 byte

अगर यह structure होता, तो कुल memory 4 + 4 + 1 = 9 bytes (padding के साथ 12 bytes) लगती। लेकिन Union में compiler सबसे बड़े member (4 bytes) के आकार का एक ही साझा memory block बनाएगा।

+-------------------------------------------------------+
|              Shared Memory Block (4 Bytes)            |
|                                                       |
|  [Byte 0]     [Byte 1]       [Byte 2]       [Byte 3]  |
|  <----------------- int roll_no --------------------> |
|  <---------------- float percentage ----------------> |
|  <- char grade ->                                     |
+-------------------------------------------------------+

Table 1: Structure vs Union (तुलना चार्ट)

तुलना का बिंदु Structure (struct) Union (union)
Keyword struct union
Memory Allocation प्रत्येक member को अलग-अलग memory space मिलता है सभी members एक ही साझा memory location शेयर करते हैं
Variable का Size सभी members के sizes का योग (padding के साथ) सबसे बड़े member के size के बराबर
Member Access एक साथ सभी members की values access की जा सकती हैं एक समय में केवल एक ही member की मान्य value रहती है
Value Overwrite एक member बदलने से दूसरे पर कोई असर नहीं पड़ता नया member assign करने पर पुराना member overwrite हो जाता है
Memory Consumption अधिक memory खर्च होती है न्यूनतम memory में काम हो जाता है (Memory efficient)

Table 2: Data Types और Union Memory Calculation

Union Members Data Type व्यक्तिगत Size (32/64 bit GCC) Union का कुल Size (sizeof)
int a; float b; int (4 bytes), float (4 bytes) 4, 4 4 bytes
char name[32]; int id; char array (32 bytes), int (4 bytes) 32, 4 32 bytes
double salary; int age; char ch; double (8 bytes), int (4 bytes), char (1 byte) 8, 4, 1 8 bytes
short code; char tag[10]; short (2 bytes), char array (10 bytes) 2, 10 10 bytes (alignment padding के साथ 10/12)

Solved Program 1: Union में Value Overwriting का Real Example

नीचे दिया गया C program दिखाता है कि कैसे union में एक member को value assign करने के बाद जब दूसरा member assign किया जाता है, तो पहला data करप्ट हो जाता है:

#include <stdio.h>
#include <string.h>

union Student {
    int roll_no;
    float marks;
    char name[20];
};

int main() {
    union Student s1;

    printf("Size of Student Union = %lu bytes\n\n", sizeof(s1));

    // Step 1: Assign roll_no
    s1.roll_no = 101;
    printf("s1.roll_no = %d\n", s1.roll_no);

    // Step 2: Assign marks (अब roll_no overwrite हो जाएगा)
    s1.marks = 89.5;
    printf("s1.marks   = %.2f\n", s1.marks);
    printf("s1.roll_no (after assigning marks) = %d  [Corrupted!]\n\n", s1.roll_no);

    // Step 3: Assign name (अब marks overwrite हो जाएगा)
    strcpy(s1.name, "Rakesh");
    printf("s1.name    = %s\n", s1.name);
    printf("s1.marks (after assigning name)    = %.2f [Corrupted!]\n", s1.marks);

    return 0;
}

Program का Real Output:

Size of Student Union = 20 bytes

s1.roll_no = 101
s1.marks   = 89.50
s1.roll_no (after assigning marks) = 1119027200  [Corrupted!]

s1.name    = Rakesh
s1.marks (after assigning name)    = 69471960398264222613504.00 [Corrupted!]

Solved Program 2: Union का सही उपयोग (One Member at a Time)

Union का सही इस्तेमाल करने का नियम यह है कि जब भी किसी member में value डालें, तो अगला member assign करने से पहले उसका काम समाप्त कर लें:

#include <stdio.h>

union DataItem {
    int id;
    float price;
};

int main() {
    union DataItem item;

    // Use Integer
    item.id = 501;
    printf("Item ID: %d\n", item.id);

    // Use Float independently
    item.price = 249.99;
    printf("Item Price: %.2f\n", item.price);

    return 0;
}

Output:

Item ID: 501
Item Price: 249.99
Advertisement

Union का Practical उपयोग कहाँ होता है? (Applications of Union)

  • Embedded Systems और Microcontrollers: IoT devices और microcontrollers (जैसे Arduino, 8051) में RAM बहुत limited (जैसे 2 KB) होती है। वहाँ एक ही memory buffer को अलग-अलग sensors के लिए बारी-बारी use करने के लिए union सबसे अच्छा विकल्प है।
  • Hardware Register Manipulation: 16-bit register को एक साथ पूरे word (int) के रूप में पढ़ना या दो अलग 8-bit bytes (High Byte और Low Byte) के रूप में access करने के लिए union का व्यापक उपयोग होता है।
  • Network Packet Parsing: जब नेटवर्क से आने वाले packet header का format protocol के अनुसार बदलता है, तो union के माध्यम से dynamically decode किया जाता है।

Students से होने वाली आम गलतियाँ (Common Mistakes)

  • सभी members को एक साथ प्रिंट करने की कोशिश करना: छात्र अक्सर सोचते हैं कि union में roll number, marks और name तीनों एक साथ print होंगे। ऐसा करने पर अंतिम member को छोड़कर बाकी सब में garbage value आएगी।
  • Structure समझकर memory जोड़ना: परीक्षा में sizeof(union) पूछते समय सभी members के sizes को जोड़ना गलत है। केवल सबसे बड़े member का size उत्तर होगा।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Union क्या है? इसका आकार (size) कैसे निर्धारित होता है?
    (उत्तर: Union एक user-defined data type है जिसमें सभी members एक memory location शेयर करते हैं। इसका आकार सबसे बड़े member के बराबर होता है।)
  2. Union में डॉट (.) और एरो (->) ऑपरेटर का उपयोग कब किया जाता है?
    (उत्तर: साधारण union variable के लिए dot ऑपरेटर और union pointer के लिए arrow ऑपरेटर का use होता है।)
  3. Structure और Union में मुख्य अंतर क्या है?
    (उत्तर: Structure में हर member को अलग memory मिलती है, जबकि Union में सभी members एक ही memory space शेयर करते हैं।)
  4. यदि एक union में int x (4 bytes) और double y (8 bytes) हैं, तो sizeof क्या होगा?
    (उत्तर: 8 bytes, क्योंकि double सबसे बड़ा member है।)

Long Questions (5–10 marks)

  1. C भाषा में Union को समझाइए। Structure और Union के बीच अंतर को उदाहरण और मेमोरी लेआउट डायग्राम के साथ स्पष्ट कीजिए। (10 marks)
  2. Union में data overwriting की समस्या क्या है? एक उपयुक्त C program लिखकर समझाइए कि union के members को सही तरीके से कैसे access किया जाता है। (10 marks)
  3. Embedded systems और network programming में Union के व्यावहारिक उपयोगों का वर्णन कीजिए। (5 marks)

FAQs

क्या Union के अंदर Structure बनाया जा सकता है?

हाँ, बिल्कुल। इसे Structure within Union या Tagged Union कहते हैं। इसका use तब किया जाता है जब हमें एक flag के आधार पर अलग-अलग structures को एक ही memory में रखना हो।

क्या Union members को एक साथ initialize किया जा सकता है?

नहीं। C standard के अनुसार, union declaration के समय आप केवल उसके पहले member को ही initial value दे सकते हैं (जैसे union Data d = {10};)।

Union में pointers का उपयोग कैसे किया जाता है?

Union का pointer बनाने पर उसके members को access करने के लिए एरो ऑपरेटर -> का use किया जाता है (जैसे ptr->roll_no)।

अगर Union में एक member बदलें तो दूसरे पर क्या असर होता है?

चूँकि सभी members एक ही memory location के bytes शेयर करते हैं, इसलिए एक member में नया मान लिखने पर पुराने member का binary data overwrite हो जाता है और वह corrupt हो जाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: C language में Union एक user-defined data type है जो विभिन्न प्रकार के data types (int, float, char आदि) को एक ही memory location में store करने की अनुमति देता है।
C Language Notes in Hindi | C language के नोट्स हिंदी में के पाठ्यक्रम में union in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको union in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement