0% found this document useful (0 votes)
19 views3 pages

C Union

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 a time. Unions are defined using the 'union' keyword and can be accessed using the '.' or '->' operators. They are memory efficient as they share the same memory space among members, contrasting with structures that allocate separate memory for each member.

Uploaded by

vjvct2354
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)
19 views3 pages

C Union

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 a time. Unions are defined using the 'union' keyword and can be accessed using the '.' or '->' operators. They are memory efficient as they share the same memory space among members, contrasting with structures that allocate separate memory for each member.

Uploaded by

vjvct2354
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/ 3

Union in C:

In C, a union is a special data type that allows you to store different data types in the same memory
location. A union can contain multiple members, but only one member can hold a value at any given time.
This is useful for memory efficiency, as the union uses the size of its largest member.

Union Definition Syntax


union UnionName {
dataType1 member1;
dataType2 member2;
// ... more members
};

Different Ways to Define 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.

1. Defining Union Variable with Declaration


union union_name {
datatype member1;
datatype member2;
...
} var1, var2, ...;

2. Defining Union Variable after Declaration


union union_name var1, var2, var3...;
where union_name is the name of an already declared union.

Access Union Members


We use the . operator to access members of a union. And to access pointer variables, we use the ->
operator.
var1.member1;
where var1 is the union variable and member1 is the member of the union.
The above method of accessing the members of the union also works for the nested unions.
var1.member1.memberA;

Here,
var1 is a union variable.
member1 is a member of the union.
memberA is a member of member1.

Initialization of Union in C
The initialization of a union is the initialization of its members by simply assigning the value to it.
var1.member1 = some_value;
Key Points

1. Memory Size: The size of a union is determined by the size of its largest member.
2. Shared Memory: All members share the same memory location, so changing one member's value
affects the others.
3. Use Cases: Unions are often used in scenarios where a variable can take on multiple types, such as in
embedded programming or when interfacing with hardware.

Example1:
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
// when j.workerNo is assigned a value, j.salary will no longer hold 12.3
j.workerNo = 100;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}

Output:
Salary = 0.0
Number of workers = 100

Example2: Accessing members of union using pointers


#include <stdio.h>

// Define a union
union Data {
int num;
char ch;
};

int main() {
// Declare a union variable
union Data data;

// Declare a pointer to the union


union Data *ptr;

// Assign the address of the union variable to the pointer


ptr = &data;
// Access and modify union members using the pointer
ptr->num = 100;
printf("num: %d\n", ptr->num);
ptr->ch = 'A';
printf("ch: %c\n", ptr->ch);
return 0;
}

When you run this program, it will output:

num: 100

ch: A

Difference between Structure and Union


Property Union Structure

A union shares the memory space among its


Members of structure do not share
members so no need to allocate memory to all the
Memory memory. So A structure need separate
members. Shared memory space is allocated i.e.
Allocation memory space for all its members i.e. all
equivalent to the size of member having largest
the members have unique storage.
memory.

To define Structure, ‘struct’ keyword is


Keyword To define Union, ‘union’ keyword is used.
used.

All members of structure can be


Initialization Only the first member of Union can be initialized.
initialized.

Size of union is equivalent to the size of the Size of the structure is > to the sum of
Size
member having largest size. the each member’s size.

Member At a time, only one member of union can be Members of structure can be accessed
Access accessed. individually at any time.

Similarities between Structure and Union


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.

You might also like