C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.
- Union and structure in C are same in concepts, except allocating memory for their members.
- Structure allocates storage space for all its members separately.
- Whereas, Union allocates one common storage space for all its members
- We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.
- Many union variables can be created in a program and memory will be allocated for each union variable separately.
- Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Using normal variable | Using pointer variable |
Syntax: union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Syntax: union tag_name { data type var_name1; data type var_name2; data type var_name3; }; |
Example: union student { int mark; char name[10]; float average; }; |
Example: union student { int mark; char name[10]; float average; }; |
Declaring union using normal variable: union student report; |
Declaring union using pointer variable: union student *report, rep; |
Initializing union using normal variable: union student report = {100, “Mani”, 99.5}; |
Initializing union using pointer variable: union student rep = {100, “Mani”, 99.5}; report = &rep; |
Accessing union members using normal variable: report.mark; report.name; report.average; |
Accessing union members using pointer variable: report -> mark; report -> name; report -> average; |
Example program for C union:
1: #include <stdio.h>
2: #include <string.h>
3: union student
4: {
5: char name[20];
6: char subject[20];
7: float percentage;
8: };
9: int main()
10: {
11: union student record1;
12: union student record2;
13: // assigning values to record1 union variable
14: strcpy(record1.name, "Raju");
15: strcpy(record1.subject, "Maths");
16: record1.percentage = 86.50;
17: printf("Union record1 values example\n");
18: printf(" Name : %s \n", record1.name);
19: printf(" Subject : %s \n", record1.subject);
20: printf(" Percentage : %f \n\n", record1.percentage);
21: // assigning values to record2 union variable
22: printf("Union record2 values example\n");
23: strcpy(record2.name, "Mani");
24: printf(" Name : %s \n", record2.name);
25: strcpy(record2.subject, "Physics");
26: printf(" Subject : %s \n", record2.subject);
27: record2.percentage = 99.50;
28: printf(" Percentage : %f \n", record2.percentage);
29: return 0;
30: }
Union record1 values example Name : Subject : Percentage : 86.500000; Union record2 values example Name : Mani Subject : Physics Percentage : 99.500000 |
Explanation for above C union program:
There are 2 union variables declared in this program to understand the difference in accessing values of union members.
Record1 union variable:
- “Raju” is assigned to union member “record1.name” . The memory location name is “record1.name” and the value stored in this location is “Raju”.
- Then, “Maths” is assigned to union member “record1.subject”. Now, memory location name is changed to “record1.subject” with the value “Maths” (Union can hold only one member at a time).
- Then, “86.50” is assigned to union member “record1.percentage”. Now, memory location name is changed to “record1.percentage” with value “86.50”.
- Like this, name and value of union member is replaced every time on the common storage space.
- So, we can always access only one union member for which value is assigned at last. We can’t access other member values.
- So, only “record1.percentage” value is displayed in output. “record1.name” and “record1.percentage” are empty.
Here
I am requesting if you found this post was helpful for you then let me
know by your comment in below comment box and share it with your friends.
Also you can contact me on Instagram @rajusunagar_
If
you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thank you!!.......
0 Comments