Union.pdf
Union.pdf
Agenda
• Union
• Defining a Union
• Memory Allocation
• Accessing Union Members
• Structure Vs Union
• References
Union
• The memory occupied by a union will be large enough to hold the largest member of
the union.
• For example, here, Exdata type will occupy 20 bytes of memory space because this
is the maximum space which can be occupied by a character string.
Memory Allocation
union Example
{
int m;
float x;
char c;
} u;
union Exdata
{
int i; OUTPUT:
float f; Memory size occupied by data : 20
char str[20];
};
int main( )
{
union Exdata data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Accessing Union Members
• To access any member of a union, we use the member access operator (.).
• The member access operator is coded as a period between the union variable name
and the union member that we wish to access.
• You would use the keyword union to define variables of union type.
Example Program 2
#include <stdio.h>
#include <string.h>
union Exdata
{
int i; OUTPUT:
float f;
char str[20]; data.i : 1917853763
}; data.f : 4122360580327794860452759994368.000000
data.str : C Programming
int main( )
{
union Exdata data;
data.i = 10;
data.f = 220.5; Note:
strcpy( data.str, "C Programming"); Here, we can see that the values of i and f members of union got
printf( "data.i : %d\n", data.i); corrupted because the final value assigned to the variable has occupied
printf( "data.f : %f\n", data.f); the memory location and this is the reason that the value of str member
printf( "data.str : %s\n", data.str); is getting printed very well.
return 0;
}
Example Program 3
#include <stdio.h>
#include <string.h>
union Exdata
{
int i; OUTPUT:
float f;
char str[20]; data.i : 10
}; data.f : 220.5
data.str : C Programming
int main( )
{
union Exdata data;
data.i = 10;
printf( "data.i : %d\n", data.i); Note:
data.f = 220.5; Here, all the members are getting printed very well because one member
printf( "data.f : %f\n", data.f); is being used at a time.
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
Structure Vs Union
Similarities
1.Both are user-defined data types used to store data of different types as a single unit.
2.Their members can be objects of any type, including other structures and unions or
arrays. A member can also consist of a bit field.
3. Both structures and unions support only assignment = and sizeof operators. The two
structures or unions in the assignment must have the same members and member types.
4. A structure or a union can be passed by value to functions and returned by value by
functions. The argument must have the same type as the function parameter. A structure
or union is passed by value just like a scalar variable as a corresponding parameter.
5.‘.’ operator is used for accessing members.
Differences
Excercise
• Write a C Program to display employee details using union.
• Tip:
union employee
{
int code;
char name[10];
int bpay;
}u[5];
References
• https://www.tutorialspoint.com/cprogramming/c_unions.htm#:~:text=A%20union%
20is%20a%20special,memory%20location%20for%20multiple%2Dpurpose.
THANK YOU