Union (यूनियन) in C Language – Definition, Syntax, Initialization and Examples
Introduction to Union
A union is a user-defined data type in C language. It is also known as a heterogeneous or non-homogeneous data type because it can contain members of different data types.
Union is used to group different types of data elements under a single name. It is declared using the union keyword.
C लैंग्वेज में यूनियन (Union) एक यूजर डिफाइन्ड डाटा टाइप है। इसे हेटेरोजिनस या नॉन-होमोजिनस डाटा टाइप भी कहा जाता है, क्योंकि इसमें विभिन्न डाटा टाइप के मेम्बर्स को एक साथ डिफाइन किया जा सकता है।
यूनियन को union कीवर्ड की सहायता से बनाया जाता है। इसका प्रयोग विभिन्न प्रकार के डाटा को एक ही नाम के अंतर्गत संग्रहित करने के लिए किया जाता है।
The main difference between a structure and a union is that in a structure, each member has its own memory location, whereas in a union, all members share the same memory area.
स्ट्रक्चर तथा यूनियन में मुख्य अंतर यह है कि स्ट्रक्चर में प्रत्येक मेम्बर के लिए अलग-अलग मेमोरी आवंटित होती है, जबकि यूनियन में सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं।
Therefore, a union is useful when a program needs to store different types of data in the same memory location, but only one member's value is required at a time.
इसलिए यूनियन का प्रयोग तब किया जाता है, जब किसी प्रोग्राम में विभिन्न प्रकार के डाटा को एक ही मेमोरी स्थान पर संग्रहित करना हो तथा एक समय में केवल एक मेम्बर की value की आवश्यकता हो।
Definition of Union
A union is a user-defined data type in C language in which all members share the same memory location. It allows different data types to be represented using a single variable.
यूनियन C लैंग्वेज का एक यूजर डिफाइन्ड डाटा टाइप है, जिसमें सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं। इसके द्वारा विभिन्न डाटा टाइप को एक ही वेरिएबल के माध्यम से represent किया जा सकता है।
Declaration of Union
The general syntax of union declaration is:
union union_name
{
datatype member1;
datatype member2;
datatype member3;
.
.
datatype memberN;
};यूनियन डिक्लेरेशन का सामान्य प्रारूप निम्नलिखित है:
union union_name
{
datatype member1;
datatype member2;
datatype member3;
.
.
datatype memberN;
};Here,
unionis a keyword used to define a union.union_nameis the name of the union.datatyperepresents the data type of a member.member1,member2, etc. are the members of the union.
यहाँ union एक कीवर्ड है, जिसका प्रयोग यूनियन बनाने के लिए किया जाता है। union_name यूनियन का नाम होता है तथा member1, member2 आदि यूनियन के मेम्बर होते हैं।
Example of Union Declaration
union student
{
int rollno;
char name[50];
char gender;
float fees;
};In this example, a union named student is defined.
इस उदाहरण में student नाम का एक यूनियन बनाया गया है।
The union contains four members:
| Member | Data Type | Description |
|---|---|---|
| rollno | int | Stores roll number |
| name | char[50] | Stores name |
| gender | char | Stores gender |
| fees | float | Stores fees |
इस यूनियन में चार मेम्बर्स हैं। rollno एक integer मेम्बर है, name एक character array है, gender एक character मेम्बर है तथा fees एक float मेम्बर है।
However, all these members share the same memory location.
लेकिन इन सभी मेम्बर्स के लिए एक ही मेमोरी स्थान होता है।
Union Variable
A union variable is a variable declared using a union data type. It is used to store the actual data of a union.
यूनियन वेरिएबल वह वेरिएबल होता है, जिसे यूनियन डाटा टाइप की सहायता से डिक्लेयर किया जाता है। इसका प्रयोग यूनियन के वास्तविक डाटा को संग्रहित करने के लिए किया जाता है।
Syntax
union union_name variable_name;Multiple union variables can also be declared in a single statement.
union union_name var1, var2, var3;Example
union student s1;
union student s2;Or,
union student s1, s2;In this example, s1 and s2 are union variables of type student.
इस उदाहरण में s1 तथा s2 दोनों student यूनियन टाइप के वेरिएबल हैं।
Each union variable has its own memory area, but the members inside each union variable share that memory area.
प्रत्येक यूनियन वेरिएबल के लिए अलग मेमोरी आवंटित होती है, लेकिन उस वेरिएबल के सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं।
Union Declaration with Variables
A union can also be declared along with its variables.
यूनियन को उसके वेरिएबल्स के साथ भी डिक्लेयर किया जा सकता है।
Syntax
union union_name
{
datatype member1;
datatype member2;
datatype member3;
} var1, var2, var3;Example
union student
{
int rollno;
char name[50];
char gender;
float fees;
} s1, s2;In this example, the union student and two union variables s1 and s2 are declared together.
इस उदाहरण में student नाम का यूनियन तथा उसके दो वेरिएबल s1 और s2 एक साथ डिक्लेयर किए गए हैं।
Memory Allocation in Union
A union definition describes the members of the union. Memory is allocated when a union variable is created.
यूनियन डिफाइन करने पर उसके मेम्बर्स का प्रारूप तैयार होता है। जब यूनियन का वेरिएबल बनाया जाता है, तब उसके लिए मेमोरी आवंटित होती है।
In a union, all members share the same memory location. The size of a union is sufficient to accommodate its largest member, with possible additional padding for alignment.
यूनियन में सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं। यूनियन का साइज सामान्यतः उसके सबसे बड़े मेम्बर के साइज के बराबर या उससे अधिक होता है। कम्पाइलर द्वारा memory alignment के लिए अतिरिक्त padding भी जोड़ी जा सकती है।
Therefore, the size of a union is not the sum of the sizes of all its members.
इसलिए यूनियन का कुल साइज उसके सभी मेम्बर्स के साइज के योग के बराबर नहीं होता है।
Example
union data
{
int a;
float b;
char c;
};In this example, the union contains three members:
aof typeintbof typefloatcof typechar
All three members share the same memory area.
इस उदाहरण में a, b तथा c तीनों एक ही मेमोरी स्थान को शेयर करते हैं।
The size of the union can be checked using the sizeof operator.
यूनियन का साइज sizeof ऑपरेटर की सहायता से ज्ञात किया जा सकता है।
printf("%zu", sizeof(union data));Initialization of Union
When an initial value is provided to a union variable at the time of its declaration, it is called union initialization.
यूनियन वेरिएबल को डिक्लेयर करते समय प्रारंभिक मान प्रदान करना यूनियन इनिशियलाइजेशन कहलाता है।
In C, a union can be initialized using a brace-enclosed initializer. By default, the first member is initialized.
C लैंग्वेज में यूनियन को brace-enclosed initializer की सहायता से initialize किया जा सकता है। सामान्य रूप से पहला मेम्बर initialize किया जाता है।
Syntax
union union_name variable_name =
{
value
};Example
union student
{
int rollno;
char name[50];
char gender;
float fees;
};
union student s1 = {1010};In this example, the first member rollno is initialized with the value 1010.
इस उदाहरण में यूनियन के पहले मेम्बर rollno को 1010 value से initialize किया गया है।
Designated Initializer
A specific member of a union can also be initialized using a designated initializer.
यूनियन के किसी विशेष मेम्बर को designated initializer की सहायता से initialize किया जा सकता है।
Example:
union student s1 = {.fees = 600.60};In this example, the fees member is initialized with the value 600.60.
इस उदाहरण में fees मेम्बर को 600.60 value से initialize किया गया है।
Accessing Union Members
The members of a union can be accessed using the dot (.) operator. This operator is called the member access operator.
यूनियन के मेम्बर्स को एक्सेस करने के लिए dot (.) ऑपरेटर का प्रयोग किया जाता है। इसे member access operator कहा जाता है।
Syntax
union_variable.member_nameExample
s1.rollno
s1.name
s1.gender
s1.feesHere, s1 is a union variable and rollno, name, gender, and fees are its members.
यहाँ s1 एक यूनियन वेरिएबल है तथा rollno, name, gender और fees इसके मेम्बर्स हैं।
Example Program: Union in C
The following program demonstrates the declaration, initialization, and accessing of union members.
#include <stdio.h>
union data
{
int a;
float b;
char c;
};
int main()
{
union data d;
d.a = 10;
printf("Value of a = %d\n", d.a);
d.b = 20.5;
printf("Value of b = %.2f\n", d.b);
d.c = 'A';
printf("Value of c = %c\n", d.c);
return 0;
}Output
Value of a = 10
Value of b = 20.50
Value of c = AExplanation
In this program, a union named data is defined with three members: a, b, and c.
इस प्रोग्राम में data नाम का एक यूनियन बनाया गया है, जिसमें तीन मेम्बर्स a, b तथा c हैं।
The variable d is declared as a union variable.
यहाँ d एक यूनियन वेरिएबल है।
First, the value 10 is assigned to member a.
सबसे पहले a मेम्बर में 10 value assign की गई है।
Then, the value 20.5 is assigned to member b.
इसके बाद b मेम्बर में 20.5 value assign की गई है।
Finally, the character 'A' is assigned to member c.
अंत में c मेम्बर में 'A' character assign किया गया है।
Since all members share the same memory location, assigning a value to one member overwrites the data previously stored through another member.
चूँकि सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं, इसलिए एक मेम्बर में value assign करने पर पहले वाले मेम्बर का डाटा overwrite हो जाता है।
Therefore, the program accesses the member that was most recently assigned a value.
इसलिए प्रोग्राम में उस मेम्बर को access किया जाता है, जिसे सबसे अंत में value assign की गई है।
Union with Structure
A union can also be used as a member of a structure, and a structure can be used as a member of a union.
यूनियन को स्ट्रक्चर के मेम्बर के रूप में तथा स्ट्रक्चर को यूनियन के मेम्बर के रूप में भी प्रयोग किया जा सकता है।
Example:
struct student
{
int rollno;
char name[50];
};
union record
{
struct student s;
float fees;
};In this example, the union record contains a structure member s and a float member fees.
इस उदाहरण में record यूनियन के अंदर s नाम का एक स्ट्रक्चर मेम्बर तथा fees नाम का एक float मेम्बर है।
Union Pointer
A pointer can also be declared to point to a union variable. A union pointer stores the address of a union object.
यूनियन वेरिएबल के address को संग्रहित करने के लिए union pointer का प्रयोग किया जा सकता है।
Syntax
union union_name *pointer_name;Example
union data
{
int a;
float b;
};
union data d;
union data *ptr;
ptr = &d;In this example, ptr is a pointer to a union of type data. It stores the address of union variable d.
इस उदाहरण में ptr एक union pointer है, जो d वेरिएबल का address संग्रहित करता है।
Union members can be accessed through a pointer using the arrow (->) operator.
Union pointer की सहायता से मेम्बर्स को एक्सेस करने के लिए arrow (->) ऑपरेटर का प्रयोग किया जाता है।
ptr->a
ptr->bThe arrow operator is equivalent to dereferencing the pointer and accessing the member.
Arrow ऑपरेटर pointer को dereference करके उसके member को access करने के समान होता है।
For example:
ptr->ais equivalent to:
(*ptr).aDifference between Structure and Union
Structure and union are both user-defined data types in C language. However, they differ in memory allocation and usage.
स्ट्रक्चर तथा यूनियन दोनों C लैंग्वेज के यूजर डिफाइन्ड डाटा टाइप हैं। लेकिन इनके मेमोरी एलोकेशन तथा प्रयोग में अंतर होता है।
| Structure | Union |
|---|---|
Declared using struct keyword | Declared using union keyword |
| Each member has its own storage | All members share the same storage |
| Size depends on members and padding | Size accommodates the largest member and padding |
| Members can hold values simultaneously | Only one member's value is intended to be used at a time |
| Changing one member does not overwrite another member | Writing to one member affects the shared storage |
| Used to store complete records | Used when alternative data representations share storage |
स्ट्रक्चर में सभी मेम्बर्स के लिए अलग-अलग मेमोरी आवंटित होती है, जबकि यूनियन में सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं।
स्ट्रक्चर का प्रयोग तब किया जाता है, जब सभी मेम्बर्स का डाटा एक साथ संग्रहित करना हो। यूनियन का प्रयोग तब किया जाता है, जब विभिन्न प्रकार के डाटा में से किसी एक को एक समय में संग्रहित करना हो।
Advantages of Union
The main advantages of union are:
It allows different data types to share the same memory location.
यह विभिन्न डाटा टाइप के मेम्बर्स को एक ही मेमोरी स्थान शेयर करने की सुविधा प्रदान करता है।
It can reduce memory usage when only one member is required at a time.
जब एक समय में केवल एक मेम्बर की आवश्यकता होती है, तो यह मेमोरी की बचत कर सकता है।
It is useful for representing alternative data types.
इसका प्रयोग वैकल्पिक डाटा टाइप को represent करने के लिए किया जा सकता है।
It can be used in embedded systems and low-level programming.
इसका प्रयोग embedded systems तथा low-level programming में किया जा सकता है।
It provides a convenient way to organize related data members.
यह संबंधित डाटा मेम्बर्स को व्यवस्थित करने की सुविधा प्रदान करता है।
Important Points about Union
Union is a user-defined data type in C language.
The
unionkeyword is used to define a union.A union can contain members of different data types.
All members of a union share the same memory location.
The size of a union is sufficient to accommodate its largest member, with possible padding.
Union members are accessed using the dot (
.) operator.Union members can be accessed through a pointer using the arrow (
->) operator.A union can be initialized using a brace-enclosed initializer.
A union is useful when only one of its alternative members is needed at a time.
यूनियन C लैंग्वेज का एक यूजर डिफाइन्ड डाटा टाइप है। इसे union कीवर्ड की सहायता से बनाया जाता है। इसमें विभिन्न डाटा टाइप के मेम्बर्स हो सकते हैं, लेकिन सभी मेम्बर्स एक ही मेमोरी स्थान को शेयर करते हैं।
Conclusion
Union is an important user-defined data type in C language. It is used to store different types of data using a shared memory location.
यूनियन C लैंग्वेज का एक महत्वपूर्ण यूजर डिफाइन्ड डाटा टाइप है। इसका प्रयोग विभिन्न डाटा टाइप के डाटा को एक ही मेमोरी स्थान पर संग्रहित करने के लिए किया जाता है।
The main advantage of union is memory sharing. Unlike a structure, a union does not provide separate storage for each member. It is useful when a program needs to represent different alternative data types using the same memory area.
यूनियन का मुख्य लाभ मेमोरी शेयरिंग है। स्ट्रक्चर के विपरीत, यूनियन में प्रत्येक मेम्बर के लिए अलग मेमोरी नहीं होती है। इसका प्रयोग तब किया जाता है, जब किसी प्रोग्राम में एक ही मेमोरी स्थान का उपयोग विभिन्न वैकल्पिक डाटा टाइप के लिए करना हो।
Comments
Post a Comment