Union in C Programming | सी यूनियन हिन्दी में | C in Hindi


C programming में union विशेष प्रकार का data type होता हैं जो की एक user defined data type होता हैं । इससे पहले tutorial में हमने C structure data type के बारे में सीखा था और समझा था यदि आपको union को अच्छ से और आसानी से समझना हैं तो पहले C structure data type के बारे में जान ले क्यूकी structure और union में बहुत समानता हैं जिससे C language में union को सीखना काफी आसान हो जाएगा । 

Union in C Programming | Hindi 

Union एक derived data type होता हैं जो structure data type की तरह अलग अलग data types के data members को contain करने के लिए उपयोग किया जाता हैं । Derived data type के बारे में हमने C data types वाले tutorial में सीखा था । derived data types में वे data type आते हैं जिन्हे programmers खुद बनाते हैं जिन्हे हम user defined data type भी कहते हैं इन्ही में से एक union डाटा टाइप हैं जो की अलग अलग data type से बने variables को contain करता हैं । 
जब हमने structure में सीखा था तो structure में सभी structure members को unique memory space दिया जाता हैं और union में सभी union members एक ही memory location को share करते हैं । 
साधारण हिन्दी भाषा में समझे तो union के सभी members जिन्हे हम union members भी कह सकते हैं वो सब एक ही memory location share करते हैं परन्तु structure में इसका उल्टा हैं सभी structure members को अलग अलग memory space मिलता हैं । 
Union से केवल एक समय में एक ही member को access कर सकते हैं। 

Union Declaration Syntax in C language 

Union को declare करने के लिए union keyword का उपयोग किया जाता हैं । जिस तरह से structure को declare करने के लिए struct keyword का उपयोग किया जाता हैं । 
C programming language में keywords दिये गए हैं keywords के बारे में हमने C tokens वाले tutorial में सीखा था । 
चलिये union के syntax से समझते हैं । 

union union_name 
{
   Data_Type member1_name;
   Data_Type member2_name;
   Data_Type member3_name;
   ….
……
data_type member(n)_name;
};

या 

union union_name 
{
   Data_Type member1_variable_name;
   Data_Type member2_variable_name;
   Data_Type member3_variable_name;
   ….
……
data_type member(n)_variable_name;
};


उपर दिये गए syntax में union एक C programming का keyword हैं जो union को define करने के लिए उपयोग में लाया जाता हैं । union_name यहाँ पर union data type का एक नाम हैं जो की reserved word को छोड़के कुछ भी लिखा जा सकता हैं । Data_type अलग अलग data type हो सकते हैं जैसे int,char,float,double आदि । इसमे अलग अलाग डाटा का संग्रह(group) होता हैं । member_name या member_variable_name दोनों एक ही बात हैं जिसका मतलब हैं अलग अलग तरह के variables बना सकते हैं जैसे- name,age,salary आदि । union के अंदर जो ये variables होते हैं या कह सकते हैं elements होते हें union members कहलाते हैं । 

Declaring union in C programming 

Union के syntax को समझने के बाद C programming में union को declare करना बहुत ही आसान हो जाता हैं । 
Union को declare करने के लिए union keyword का उपयोग किया जाता हैं जैसा की आप नीचे दिये गए उदाहरण में देख सकते हैं । 

union employees
{
    int id_no;
    char name[20];
       float height;
};

या 

union users
{
    int user_id;
    char user_name[20];
};


उपर दिये गए उदाहरण में आप देख सकते हैं की किस तरह से union keyword के साथ union data type का नाम दिया गया हैं । उपर दो उदाहरण हैं जो की हमने structure वाले tutorial में भी लिए थे फर्क इतना हैं की वहाँ पर structure था यहाँ पर union हैं जिससे आसानी से आप दोनों data types को समझ सकते हैं या compare भी कर सकते हैं । 
उपर दिये गए उदाहरण में name ,id_no,height ये सभी variables हैं जिनका अलग अलग data type हैं इन सभी को union members कहते हैं । यहाँ पर employees union data type का नाम हैं । 
इस तरह से दूसरे उदाहरण में भी समान तरीके से समझ सकते हैं । 

Declaring union variables in C language 

जैसे की आप जानते हैं की जब हम variable declare करते थे variable वाले tutorial में तब variable के नाम से पहले data type आता था ठीक उसी प्रकार बिना variable के data type का कोई काम का नही क्योकि हमने अभी तक union data type बनाना सीखा परन्तु डाटा टाइप को उपयोग करने के लिए variable भी declare करना पड़ता हैं जैसे अब तक करते आ रहे थे की पहले data type फिर variable का नाम ऐसे ही अब union data type तो बन चुका अब उसके बाद variable का नाम declare कर देते हैं । C programming में दो तरह से union variable declare कर सकते हैं । इसी तरीके से हमने structure में भी सीखा था बस वहा पर structure था और यहाँ पर union हैं । पहले तरीके में union declaration के समय ही और दूसरे तरीके में union declaration के बाद । 

Union variables declaration with union declaration in C language 

Syntax-

union structure_name
{
    data_type member1_name;
    data_type member2_name;
    data_type member3_name;
    ...........
    ...........
    data_type member(n)_name;
}variable_name1, variable_name2, ...;


उपर दिये गए syntax में आप देख सकते हैं की variables को structure के समय ही declare कर दिया गया हैं । 
Example  – 

Union employees
{
    int id_no;
    char name[20];
       float height;
}emp1,emp2;

Union variables declaration after union declaration in C Programming 

Syntax - 

union union_name variable_name1, variable_name2, ... ;

Example – 
 

union employees
{
    int id_no;
    char name[20];
       float height;
};
union employees  emp1, emp2, ... ;


उपर दिये गए उदाहरण में हमने पहले union declare किया उसके बाद union variables declare किए ।

Initializing union members in the C language 

जिस तरह से हमने structure members को initialize करना सीखा था ठीक उसी प्रकार union members को भी initialize किया जाता हैं । 

#include<stdio.h>
union employee
{
int emp_id;
    char emp_name[40];
    float emp_rank; 
};
void main() 
{
union employee emp1 = { 123,”Mohan”, 4.5 };    //initialization of union members
}


उपर दिये गए उदाहरण में आप देख सकते हैं की हमने union members को union declaration के बाद initialize किया हैं । 
C language में union members को initialize कुछ इस प्रकार से भी कर सकते हैं जैसे नीचे आप उदाहरण देख सकते हैं । 

#include<stdio.h>
union employee
{
int emp_id;
    char emp_name[40];
    float emp_rank; 
};
void main() 
{
union employee emp1;  
emp1.emp_id=73; 
strcpy(emp1.emp_name, "Mohan") ;
emp1.emp_id=4.5; 
}

उपर दिये गए उदाहरण में आप देख सकते हैं की हमने अलग अलग union members को initialize किया वो भी dot(.) Operator के साथ । यहाँ पर strcpy() एक inbuilt (readymade) function हैं जिसके द्वारा string को char array में copy किया गया हैं ।

Union members को access करना 

Union में एक समय में केवल एक ही member को access कर सकते हैं ये नहीं हैं की एक साथ सभी member को access किया जाए जैसे की स्ट्रक्चर में सभी members को एक साथ access किया जा सकता हैं परन्तु यूनियन में नही किया जा सकता यदि union में एक से अधिक member को access करने की कोशिश करते हैं तो तब भी एक ही member access होगा बाकी को compiler छोड़ देगा । dot (.) operator के  द्वार union members को access किया जाता हैं । 
चलिये पहले dot (.) operator के द्वारा यूनियन member को access करते हैं । 

#include<stdio.h>
union user
{
int user_id;
float user_rank; 
};
void main() 
{
union user us1;  
us1.user_id=102; 
printf( "User id is %d n", us1.user_id); 
us1.user_rank=4.5;
printf( "User rank is  %f", us1.user_rank);  
}


Output - 

User id is 102 
User rank is  4.500000


उपर दिये गए उदाहरण में हमने एक समय मे एक union member को access किया जिस से कोई error या problem नही दिखाई दी C प्रोग्राम में अब हम एक साथ दो union members को access करके देखते हैं । 

#include<stdio.h>
union user
{
int user_id;
float user_rank; 
};
void main() 
{
union user us1;  
us1.user_id=102; 
us1.user_rank=4.5;
printf( "User id is %d and User rank is  %f", us1.user_id,us1.user_rank); 
}


Output -
 

User id is 1083179008 and User rank is  4.500000 


इस example में हमने 2 union members को एक साथ access करना चाहा परन्तु user_id सही नही मिलती हैं पर user_rank सही मिल जाती हैं । 
चलिये अगले उदाहरण में देखते हैं – 

#include<stdio.h>
union user
{
int user_id;
float user_rank; 
};
void main() 
{
union user us1; 
us1.user_rank=4.5;
us1.user_id=102; 
printf( "User id is %d and User rank is  %f", us1.user_id,us1.user_rank); 
}

Output - 
 

User id is 102 and User rank is  0.000000


इस उदाहरण में हमने केवल rank को id से पहले initialize किया अब हमे आईडी तो सही मिल रही है पर rank 00000 प्राप्त हो रही हैं इसका मतलब हैं की union में एक समय में एक ही member को access करना उचित होता हैं । 
क्यूकी एक साथ एक्सैस करने से values corrupt हो जाती हैं। 

Advantages of the union in C programming:-

Union के द्वारा memory सेव होती हैं क्यूकी स्ट्रक्चर में सभी members को अलग अलाग space मिलता हैं जिससे memory काफी ज्यादा उपयोग में आ जाती हैं परन्तु union में ऐसा नही union में सभी प्रकार की डाटा वैल्यू एक ही space में store होते हैं  और values का उपयोग भी कर सकते हैं एक ही जगह से चाहे कोई भी डाटा टाइप की वैल्यू ही क्यू न हो । 
Union data type की size union के सबसे बड़े member की size के बराबर होती हैं । 

Disadvantages of the union in C language:-

Structure में हम एक समय में एक से अधिक members को access कर सकते हैं पर union में केवल एक समय में एक ही member को access कर सकते हैं क्यूकी इसमे member एक ही space share  करते रहते हैं ।
Union में एक ही memory location एक से ज्यादा members में share होती हैं जिसके कारण यदि C programming में एक से अधिक members को access किया जाए तो एक को छोड़के बाकी members values corrupt हो जाती हैं । 
चलिये structure और union में बचे हुए अंतर को समझते हैं – 

Difference between union and structure in C language:-

Structure  Union 
Structure में यदि एक डाटा member की वैल्यू change करदी जाए तो इसका असर अन्य data members पर नही पड़ता ।  Union में data members में बदलाव करने पर दूसरे members पर भी इसका प्रभाव पड़ता हैं । 
Structure flexible array को support करता हैं ।  Union flexible array को support नहीं करता हैं । 
Structure की size member की size के जोड़के बराबर होती हैं ।  Union की size union में सबसे बड़े member के size के बराबर होती हैं । 
Structure में प्रत्येक मेम्बर को unique space दिया जाता हैं ।  Union मे प्रत्येक मेम्बर एक ही मेमोरी को शेर करते हैं उनको कोई unique space नही दिया जाता हैं । 

इस तरह से Union का उपयोग C programming language में किया जाता हैं जो की एक user defined data type हैं । 


Please Share

Recommended Posts:-