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

Union in C

Union allows defining a data type that can hold multiple data types in the same memory location. Only one member can contain a value at any time. Union allocates memory equal to the size of its largest member. This conserves memory compared to structures that allocate separate memory for each member. Union members are accessed similar to structures using the dot operator.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Union in C

Union allows defining a data type that can hold multiple data types in the same memory location. Only one member can contain a value at any time. Union allocates memory equal to the size of its largest member. This conserves memory compared to structures that allocate separate memory for each member. Union members are accessed similar to structures using the dot operator.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Union in c

• Union can be defined as a user-defined data type which is a collection of different


variables of different data types in the same memory location.
• The union can also be defined as many members, but only one member can contain a
value at a particular point in time.
• Union is a user-defined data type, but unlike structures, they share the same memory
location.
• To define a union, you must use the union statement in the same way as you did while
defining a structure.
• The union statement defines a new data type with more than one member for your
program.
Union is similar to the structures, it also contains number of members like
structures. In C, union is identified by the keyword union The members of union
share the same memory location within the computer, whereas each structure
member has its own memory location within the computer. At any time of
compilation, only one member of the union can reside in that memory location.
The union require bytes that are equal to the bytes required by largest member
within union. The union allocates fixed bytes. Thus, unions are used to conserve
or save memory.
union [union tag] { union Data {
member definition; int i;
member definition; float f;
... char str[20];
member definition; } data;
} [one or more union variables];
#include <stdio.h>
#include <string.h>

union Data
{
int i;
float f;
char str[20];
};

int main( )
{

union Data data;


printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
union car
{
char name[50];
int price;
};

int main()
{
union car car1, car2, *car3;
return 0;
}

Another way of creating union variables is:

union car
{
char name[50];
int price;
} car1, car2, *car3;
Accessing a Union Member

A union member can be accessed similar to structure member,

that by using (.)dot or period operator.


#include <stdio.h>
int main()
{
/structures declartion
struct sample{
int a;
int b;
}s1;
//Union declartion
union samp{
int c;
int d;
}u1;
s1.a = 10;
s1.b = 20;
printf("\nThe Structure member values a and b are : %d %d ",
s1. a, s1.b);
u1.c = 30;
u1.d = 40;
printf("\nThe Union member values c and d are : %d %d ", u1.
c, u1.d);
return 0;
}
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;

int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}

You might also like