0% found this document useful (0 votes)
3 views8 pages

Union

Unions in C are user-defined data types that allow different data types to be stored in the same memory location, with only one member able to hold a value at a time. They are declared using the 'union' keyword and can be defined in various ways, with initialization allowing only one member to be assigned a value at any moment. The size of a union is determined by its largest member, and it differs from structures in memory allocation, usage, and initialization methods.
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)
3 views8 pages

Union

Unions in C are user-defined data types that allow different data types to be stored in the same memory location, with only one member able to hold a value at a time. They are declared using the 'union' keyword and can be defined in various ways, with initialization allowing only one member to be assigned a value at any moment. The size of a union is determined by its largest member, and it differs from structures in memory allocation, usage, and initialization methods.
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/ 8

Union in C

1
What is Union in C?
 Unions in C are a user-defined data type that
permits different data types to be stored in the same
memory location.
 While similar to structures, the key difference is
that all members of a union occupy the same
memory location, which means only one member
can store a value at a time.
 Unions are primarily used when you need to work
with different data types in the same memory space
to save memory.
2
Union Declaration
 In C, you declare a union using the union keyword.
 General syntax:
 union union_name
{
// member declarations
};
EXAMPLE
union Data {
int i;
float f;
char str[20];
};
3
Different Ways to Define a
Union Variable
 There are two ways to define a union variable:
 With Union Declaration
union name_of_union {
datatype member;
} var;
 This code defines a union named name_of_union with a single member of data
type datatype. The union is named var, and it can hold data of the specified data
type in its member.
 After Union Declaration
 union name_of_union var;
 In this code, name_of_union is the name of an already declared union.

4
Initialization of Union in C

 The union is initialized by assigning values to its members.


 It's essential to note that, at any given moment, only one member can
hold a value.

 var.member = some_value;

5
Example of Union in C
1. #include <stdio.h> 11. // Assign values to different
2. // Define a union named members of the union
MyUnion 12. data.intValue = 42;
3. union MyUnion { 13. printf("Integer Value: %d\n",
4. int intValue; data.intValue);
5. float floatValue; 14. data.floatValue = 3.14;
6. char stringValue[20]; 15. printf("Float Value: %.2f\n",
7. }; data.floatValue);
8. int main() { 16. // Assign a string to the union
9. // Declare a variable of the 17. strcpy(data.stringValue,
union "Hello, Union!");
10. union MyUnion data; 18. printf("String Value: %s\n",
data.stringValue);
19. return 0;
6
20. }
Size of Union

 The size of a union in C is determined by the size of its


largest member.
 The idea is that all members of a union share the same
memory space, so the union's size needs to be large enough
to accommodate its largest member.

7
Difference between C Union and C Structure
Feature C Union C Structure
Shares memory space among all Each member has its memory
Memory Allocation
members. space.
Shares memory, using the largest Requires memory for each
Memory Usage
member's size. member simultaneously.
Members share the same memory Members are accessed
Accessing Members
space. individually.
Size is the size of the largest Size is the sum of sizes of its
Size Calculation
member. members.
No wastage as it uses the size of Can lead to memory wastage for
Memory Wastage
the largest member. small types.

All members share the same


Each member can be initialized
Initialization memory, so initializing one affects
separately.
others.

Suitable when only one of the data


Suitable when different data types
Usage types is used at a time to save
are needed.
memory.
c union Number { int x; float y;
Example 8
c struct Point { int x; int y; };
};

You might also like