0% found this document useful (0 votes)
2 views

Union.pdf

A union in C is a data type that allows storing different data types in the same memory location, with only one member holding a value at any time. Memory allocated for a union is based on its largest member, and accessing union members is done using the member access operator. Unions share similarities with structures but differ primarily in memory allocation and variable storage.

Uploaded by

whaters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Union.pdf

A union in C is a data type that allows storing different data types in the same memory location, with only one member holding a value at any time. Memory allocated for a union is based on its largest member, and accessing union members is done using the member access operator. Unions share similarities with structures but differ primarily in memory allocation and variable storage.

Uploaded by

whaters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNION

Agenda
• Union

• Defining a Union
• Memory Allocation
• Accessing Union Members
• Structure Vs Union
• References
Union

• Definition – Same as structure


• Syntax – Same as structure
• Difference in structure and
union
– Keyword – union
– Memory allocation
– Variable Storage
Union
• A union is a special data type available in C that allows to store different data types
in the same memory location.
• You can define a union with many members, but only one member can contain a
value at any given time.
• Unions provide an efficient way of using the same memory location for
multiple-purpose.
Defining a
Union
• The union statement defines a new data type with more than one member for your program.
• Syntax:
union tag_name
{
data-type Field1;
data-type Field2;
………
}[one or more union variables];
• The tag_name is optional and each member definition is a normal variable definition.
• At the end of the union's definition, before the final semicolon, you can specify one or more
union variables but it is optional.
Memory Allocation
union Exdata
{
int i;
float f;
char str[20];
} data;

• 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;

All members share the same storage area in computers memory.


Example Program1
#include <stdio.h>
#include <string.h>

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

You might also like