- Union ये एक अलग-अलग data types का collection होता है |
- अगर union का इस्तेमाल करना हो तो 'union' keyword का इस्तेमाल करते है |
- Union ये structure के जैसा ही होता है |
- Union में किये हुए हर variable के decaration को 'member' कहते है |
- Union हर एक member के लिए अलग-अलग memory allocate नहीं करता है |
- Union के members एक ही memory location को share करते है |
- Union में जो member अपने size में बड़ा होता है, तो वो पूरे Union की size होती है |
Syntax Difference between Structure and Union Defintion
Syntax for Structure Definition
struct structure_name{
data_type member 1;
data_type member 2;
------------------
------------------
data_type memeber n;
};
Syntax for Union Definition
struct union_name{
data_type member 1;
data_type member 2;
------------------
------------------
data_type memeber n;
};
Example for Structure Definition
struct Employee{
int emp_id;
char emp_name[30];
};
Example for Union Definition
union Student{
int stud_id;
char stud_name[30];
};
Memory Representation
| Structure | Union | |||||
| Structure_name | Employee | Student | ||||
| Member_name | emp_id | emp_name[30] | stud_id and stud_name[30] | Location | ||
| Memory Address | 6356728 | 6356732 | 6356762 | |||
| Member Size | 4bytes | 30bytes | 4bytes | 30bytes | ||
| Total Size | 34bytes | 30bytes | ||||
Union Variable Declaration
Structure का variable declare करने के दो प्रकार है |- जब Union की definition लिखी जाती तब वहा पर भी Structure variable को लिखा जाता है |
- Union के variable को main() function के अंदर भी किया जाता है |
Syntax for Union Variable outside of Union Definition
struct Union_name{
data_type member 1;
data_type member 2;
------------------
------------------
data_type memeber n;
}Union_variable(s);
Example for Union Variable in main() Function
struct Employee{
int emp_id;
char emp_name[30];
float salary;
}info;
Syntax for Union Variable in main() Function
union Union_name{
data_type member 1;
data_type member 2;
------------------
------------------
data_type memeber n;
}Union_variable(s);
int main(){
union Union_name Union_variable_name;
}
Example for Union Variable in main() Function
union Employee{
int emp_id;
char emp_name[30];
float salary;
};
int main(){
union Employee info;
}
Full Example for Union
Source Code :
#include <stdio.h>
#include <string.h>
union Employee {
int emp_id;
char emp_name[30];
float salary;
}info;
int main(){
info.emp_id = 34;
strcpy( info.emp_name, "Raj Biradar");
info.salary = 20000.00;
printf( "Employee id is : %d\n", info.emp_id);
printf( "Employee name is %s\n", info.emp_name);
printf( "Employee salary is : %f", info.salary);
return 0;
}
Output :
Employee id is : 1184645120 Employee name is Employee salary is : 20000.000000
Union working with size(sizeof)
Union के member एक ही memory location में store होता है और पूरे Union का size जो सबसे बड़ा member होता है, वो size Union की होती है |Source Code :
#include <stdio.h>
#include <string.h>
union Employee {
int emp_id;
char emp_name[20];
float salary;
};
int main(){
union Employee info;
printf( "Size of Employee id is : %d bytes\n", sizeof(info.emp_id)); // size of emp_id
printf( "Size of Employee name : %d bytes\n", sizeof(info.emp_name)); // size of emp_name
printf( "Size of Employee salary is : %d bytes\n", sizeof(info.salary)); // size of salary
printf( "Size of Employee union : %d bytes", sizeof(info)); // size of Employee
return 0;
}
Ouutput :
Size of Employee id is : 4 bytes Size of Employee name : 20 bytes Size of Employee salary is : 4 bytes Size of Employee union : 20 bytes
Union using Pointer
Union के Members को दो प्रकार से access किया जाता है |- . (dot Operator)
- -> (pointer Operator)
Accessing Members using pointer Operator
Source Code :
#include <stdio.h>
#include <string.h>
union Employee {
int emp_id;
char emp_name[30];
float salary;
};
int main(){
union Employee info;
union Employee *ptr;
ptr = &info;
ptr->emp_id = 34;
strcpy( ptr->emp_name, "Raj Biradar");
ptr->salary = 20000.00;
printf( "Employee id is : %d\n", ptr->emp_id);
printf( "Employee name is %s\n", ptr->emp_name);
printf( "Employee salary is : %f", ptr->salary);
return 0;
}
< /pre>
Output :
Employee id is : 1184645120 Employee name is Employee salary is : 20000.000000







No comments: