UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space...
-
Upload
hugo-rickard -
Category
Documents
-
view
231 -
download
0
Transcript of UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space...
![Page 1: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/1.jpg)
UNIONS IN C
![Page 2: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/2.jpg)
Union Data Type Defining of Union Memory Space Allocation Example of Union Result Difference between Structures & Union
![Page 3: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/3.jpg)
Union Data Type
A union is a user defined data type like structure. The union
groups logically related variables into a single unit. The union data
type allocate the space equal to space need to hold the largest
data member of union. The union allows different types of variable
to share same space in memory. There is no other difference
between structure and union than internal difference. The method
to declare, use and access the union is same as structure.
BACK
![Page 4: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/4.jpg)
Defining of Union A union has to defined, before it can used. The syntax of UNION
is:
union union_name
{
data_type-variable_name;
data_type-variable_name;
……..
data_type-variable_name;
};
![Page 5: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/5.jpg)
Example of Union
The union of Employee is declared as: union employee{int emp_id;char name[20];float salary;char address[50];int dept_no;int age; };
BACK
![Page 6: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/6.jpg)
Memory Space Allocation 8000
emp_id, dept_no, age8002
salary8004
name
8022
address
8050
BACK
![Page 7: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/7.jpg)
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
main()
{
union student record1;
union student record2;
strcpy(record1.name, "Raju");
strcpy(record1.subject, "Maths");
record1.percentage = 86.50;
![Page 8: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/8.jpg)
printf("Union record1 values example\n");
printf(" Name : %s \n", record1.name);
printf(" Subject : %s \n", record1.subject);
printf(" Percentage : %f \n\n", record1.percentage);
printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name);
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
getch();
}
BACK
![Page 9: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/9.jpg)
Result
BACK
![Page 10: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/10.jpg)
![Page 11: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/11.jpg)
BACK
![Page 12: UNIONS IN C. Union Data Type Union Data Type Defining of Union Defining of Union Memory Space Allocation Memory Space Allocation Example of Union.](https://reader036.fdocuments.net/reader036/viewer/2022062417/551c4441550346a66a8b45bd/html5/thumbnails/12.jpg)
BACK TO INDEX