KEMBAR78
Union | PDF
0% found this document useful (0 votes)
18 views3 pages

Union

In C, a union is a user-defined data type that allows storing different data types in the same memory location, with only one member being able to hold a value at a time. The document explains the syntax for declaring unions, creating union variables, accessing members, and initializing unions, along with examples demonstrating their usage. Additionally, it highlights that the size of a union is determined by the size of its largest member.

Uploaded by

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

Union

In C, a union is a user-defined data type that allows storing different data types in the same memory location, with only one member being able to hold a value at a time. The document explains the syntax for declaring unions, creating union variables, accessing members, and initializing unions, along with examples demonstrating their usage. Additionally, it highlights that the size of a union is determined by the size of its largest member.

Uploaded by

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

C Union




In C, union is a user-defined data type that can contain elements of
the different data types just like structure. But unlike structures, all
the members in the C union are stored in the same memory
location. Due to this, only one member can store data at the given
point in time.
Let’s take a look at an example:
{...}
// Union definition
union A {
int i;
float f;
char s[20];
};

int main() {

union A a;

// Storing an integer
a.i = 10;
printf("data.i = %d
", a.i);

// Storing a float
a.f = 220.5;
printf("data.f = %.2f
", a.f);

// Storing a string
strcpy(a.s, "GfG");
printf("data.s = %s
", a.s);

{...}

Output
data.i = 10
data.f = 220.50
data.s = GfG
Syntax of Union in C
The syntax of union can be defined into two parts:
C Union Declaration
In this part, we only declare the template of the union, i.e., we only
declare the members’ names and data types along with the name
of the union. No memory is allocated to the union in the
declaration.
union name {
type1 member1;
type2 member2;
.
.
};
Create a Union Variable
We need to define a variable of the union type to start using union
members. There are two methods using which we can define a
union variable:
Creating Union Variable with Declaration
union name{
type member1;
type member2;

} var1, var2, …;
Creating Union Variable after Declaration
union name var1, var2, var3…;where name is the name of an
already declared union.
Access Union Members
We can access the members of a union by using the ( . ) dot
operator just like structures.
var1.member1;
where var1 is the union variable and member1 is the member
of the union.
Initialize Union
The initialization of a union is the initialization of its members by
simply assigning the value to it.
var1.member1 = val;
One important thing to note here is that only one member can
contain some value at a given instance of time.
Size of Union
The size of the union will always be equal to the size of the
largest member of the union. All the less-sized elements can
store the data in the same space without any overflow. Let’s take a
look at the
include <stdio.h>

// Declaring multiple unions


union A{
int x;
char y;
};

union B{
int arr[10];
char y;
};

int main() {

// Finding size using sizeof() operator


printf("Sizeof A: %ld\n", sizeof(union A));
printf("Sizeof B: %ld\n", sizeof(union B));

return 0;
}

Output
Sizeof A: 4
Sizeof B: 40

You might also like