BCA / B.Tech 12 min read

union in Hindi

Union in C Language in Hindi | C भाषा में  यूनियन हिंदी में :


  • C प्रोग्रामिंग भाषा में, union एक user-defined डेटा प्रकार (data type) होता है, जो कि struct की तरह ही काम करता है, लेकिन इसमें एक महत्वपूर्ण अंतर होता है। 
  • Union का उपयोग तब किया जाता है, जब हम अलग-अलग प्रकार के डेटा को एक ही memory location में store करना चाहते हैं। 
  • Union के माध्यम से हम एक ही memory block को विभिन्न प्रकार के डेटा के लिए साझा कर सकते हैं, जिससे memory का efficient उपयोग होता है।
  • C भाषा में union एक उपयोगी डेटा संरचना है, जो memory की बचत करने में मदद करती है। इसका उपयोग तब किया जाता है, जब हमें एक ही memory स्थान में विभिन्न प्रकार के डेटा को संग्रहीत करना हो।
  •  हालाँकि, यह ध्यान में रखना आवश्यक है कि union में एक समय में केवल एक ही सदस्य की मान को सही तरीके से access किया जा सकता है। Union का उपयोग अक्सर embedded systems, hardware interfacing और network programming में किया जाता है, जहाँ memory की खपत को कम से कम रखना होता है।
Defination in C Language in Hindi | Union की परिभाषा:

  • Union एक ऐसा डेटा प्रकार है, जिसमें एक या एक से अधिक सदस्यों (members) को store करने की क्षमता होती है, लेकिन सभी सदस्यों के लिए एक ही memory location का उपयोग किया जाता है। 
  • इसका मतलब यह है कि union में एक समय में केवल एक ही सदस्य मान (value) रख सकता है, क्योंकि सभी सदस्य एक ही memory स्थान साझा करते हैं। जब एक सदस्य को मान दिया जाता है, तो उसी memory स्थान पर अन्य सदस्यों की मान को overwrite कर दिया जाता है।

Union का सिंटैक्स (Syntax):

Union को declare करने का तरीका बहुत हद तक structure की तरह होता है। इसका सिंटैक्स निम्नलिखित है:

union union_name {
    data_type1 member1;
    data_type2 member2;
    data_type3 member3;
    ...
};
यहाँ:

  • union keyword का उपयोग करके union को परिभाषित किया जाता है।
  • union_name union का नाम होता है।
  • data_type1, data_type2, आदि डेटा के प्रकार होते हैं, जो union के members होते हैं।
  • member1, member2, आदि union के सदस्य होते हैं।

उदाहरण:

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data data;

    data.i = 10;
    printf("data.i : %d\n", data.i);

    data.f = 220.5;
    printf("data.f : %f\n", data.f);

    // Notice that 'data.i' is now corrupted
    printf("data.i (after assigning float) : %d\n", data.i);

    data.str[0] = 'C';
    data.str[1] = '\0';  // Null-terminating the string
    printf("data.str : %s\n", data.str);

    // Notice that both 'data.i' and 'data.f' are now corrupted
    printf("data.i (after assigning string) : %d\n", data.i);
    printf("data.f (after assigning string) : %f\n", data.f);

    return 0;
}
आउटपुट:

data.i : 10
data.f : 220.500000
data.i (after assigning float) : 1131830528
data.str : C
data.i (after assigning string) : 67
data.f (after assigning string) : 0.000000

इस उदाहरण में, हम देख सकते हैं कि जब union के किसी एक सदस्य को मान दिया जाता है, तो अन्य सदस्यों के मान बदल जाते हैं, क्योंकि सभी सदस्य एक ही memory स्थान साझा करते हैं।

Union के साथ काम करने के मुख्य बिंदु:

  • सदस्य एक ही समय में उपयोग नहीं कर सकते (Only One Member at a Time): Union के अंदर एक ही समय में केवल एक सदस्य की मान को सही रूप से access किया जा सकता है। यदि हम एक सदस्य को मान देते हैं और फिर दूसरे सदस्य का मान access करते हैं, तो पहले सदस्य का मान खो जाएगा।
  • स्मृति स्थान (Memory Space): Union में जितने भी सदस्य होते हैं, वे सभी एक ही memory स्थान का उपयोग करते हैं। Union का कुल आकार (size) उसके सबसे बड़े सदस्य के आकार के बराबर होता है। इसका मतलब यह है कि union का आकार उसके सबसे बड़े सदस्य के आकार के बराबर होता है, चाहे union में कितने भी सदस्य क्यों न हों।
  • स्मृति की बचत (Memory Saving): Union का उपयोग मुख्य रूप से तब किया जाता है, जब memory की बचत की आवश्यकता होती है। यह structure की तुलना में memory का बेहतर उपयोग करता है, क्योंकि structure में सभी सदस्यों के लिए अलग-अलग memory allocate होती है, जबकि union में सभी सदस्यों के लिए एक ही memory location साझा किया जाता है।
Union के साथ memory allocation को समझना:

जब हम union के सदस्यों को declare करते हैं, तो compiler union का आकार सबसे बड़े सदस्य के आकार के बराबर तय करता है। उदाहरण के लिए, अगर हमारे पास union में एक int, एक float, और एक char[20] है, तो union का आकार 20 bytes होगा, क्योंकि char[20] सबसे बड़ा सदस्य है।

उदाहरण:

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data data;
    printf("Size of union: %lu\n", sizeof(data));  // Output will be 20, because 'str' is the largest member
    return 0;
}

आउटपुट:


Size of union: 20

इस उदाहरण में, union का कुल आकार 20 bytes है, क्योंकि उसका सबसे बड़ा सदस्य char[20] है।

Union का उपयोग कहाँ किया जाता है?

Union का उपयोग मुख्य रूप से तब किया जाता है जब हमें memory को efficient तरीके से उपयोग करना हो। कुछ सामान्य उपयोग के मामले निम्नलिखित हैं:

  • मेमोरी के उपयोग को अनुकूलित करना (Optimizing Memory Usage): Embedded systems जैसे low-memory devices में union का उपयोग होता है, ताकि memory को conserved किया जा सके।
  • संवेदनशील डेटा का प्रबंधन (Managing Sensitive Data): Union का उपयोग किया जा सकता है जब एक ही डेटा के विभिन्न प्रारूपों (formats) को manage करना हो। उदाहरण के लिए, एक ही data को integer, float और character array के रूप में access करना।
  • डेटा पैकिंग (Data Packing): Network programming या hardware interfacing में union का उपयोग डेटा पैकिंग और अनपैकिंग के लिए किया जा सकता है। इससे अलग-अलग प्रकार के डेटा को एक ही स्थान पर संग्रहीत किया जा सकता है और उन्हें सही क्रम में भेजा जा सकता है।
  • Hardware Interfacing: हार्डवेयर से डेटा पढ़ते समय union का उपयोग अक्सर किया जाता है, जहां डेटा के विभिन्न प्रकारों को एक ही memory स्थान से पढ़ना होता है।

Union का उदाहरण:

उदाहरण 1: अलग-अलग प्रकार के डेटा को union के माध्यम से संग्रहीत करना

#include <stdio.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data data;

    // Integer को store करना
    data.i = 10;
    printf("Integer: %d\n", data.i);

    // Float को store करना
    data.f = 220.5;
    printf("Float: %f\n", data.f);

    // String को store करना
    strcpy(data.str, "Hello");
    printf("String: %s\n", data.str);

    return 0;
}

इस उदाहरण में, हम पहले integer को store करते हैं, फिर float, और अंत में string को store करते हैं। हर बार, नई value पुराने डेटा को overwrite कर देती है।

उदाहरण 2: मेमोरी की बचत का उदाहरण

#include <stdio.h>

struct ExampleStruct {
    int i;
    float f;
    char str[20];
};

union ExampleUnion {
    int i;
    float f;
    char str[20];
};

int main() {
    struct ExampleStruct s;
    union ExampleUnion u;

    printf("Size of structure: %lu\n", sizeof(s));
    printf("Size of union: %lu\n", sizeof(u));

    return 0;
}

आउटपुट:


Size of structure: 28
Size of union: 20

यह उदाहरण यह दिखाता है कि structure में सभी सदस्यों के लिए अलग-अलग memory allocate की जाती है, जिससे structure का आकार union की तुलना में बड़ा होता है। वहीं union में memory केवल सबसे बड़े सदस्य के आकार के बराबर allocate की जाती है, जिससे memory की बचत होती है।