Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is union of structure in C language?
In C programming, a union is a memory location shared by multiple variables of different data types. While we can define multiple members in a union, only one member can hold a value at any given time. This makes unions an efficient way to use the same memory space for different purposes.
Declaration of Union Variable
A union is a user-defined datatype in the C programming language. This allows the storage of different types of variables in the same location. A union can have multiple members, and only a single member can hold a value at any given time. Declaring unions are useful for interacting with memory-mapped registers.
Syntax
The syntax for the union of structure is as follows ?
union uniontag{
datatype member 1;
datatype member 2;
----
----
datatype member n;
};
A union variable can be declared in three types. Let's look at these one by one ?
Type 1
A union named sample with three members: an integer "a", a float "b", and a char "c" are defined as ?
union sample{
int a;
float b;
char c;
}s;
Type 2
A union with three members: an integer "a", a float "b", and a char "c", named "s" are defined as ?
union{
int a;
float b;
char c;
}s;
Type 3
This defines a union named sample with members a, b, c, and specifies a variable "s" of type sample.
union sample{
int a;
float b;
char c;
};
union sample s;
When a union is declared, the compiler allocates enough memory to hold the largest member. Only one member can be accessed at a time.
Basic Usage
The following example defines a union named sample with three members: an integer a, afloat b, and a character c.
union sample{
int a;
float b;
char c;
};
Initialization and Accessing
The same syntax used for structures is applied to access union members.
-
The dot operator(.) is used to access members.
-
The arrow operator(->) is used to access members through a pointer.
Example
The following program demonstrates the usage of a union in a structure. This C program defines a union named sample with three members and prints their values after specifying different data types to each member.
#include <stdio.h>
union sample {
int a;
float b;
char c;
};
int main() {
union sample s;
s.a = 20;
printf("a = %d
", s.a);
s.b = 10.5;
printf("b = %f
", s.b);
s.c = 'A';
printf("c = %c
", s.c);
return 0;
}
Output
When the above program is executed, it produces the following result ?
a = 20 b = 10.500000 c = A
Union of Structures
A structure can be nested inside a union, known as a union of structures. Similarly, a union can be created inside a structure. To define a structure, we use the struct statement. In a union of structures, a change in the value of an individual data member does not affect the other data members of the structure.
Example
Here's another C program demonstrating the usage of a union within a structure. This program defines a union named sample containing a structure and prints the values of the structure's members.
#include <stdio.h>
struct x {
int a;
float b;
};
union z {
struct x s;
};
int main() {
union z u;
u.s.a = 10;
u.s.b = 30.5;
printf("a = %d
", u.s.a);
printf("b = %f
", u.s.b);
return 0;
}
Output
When the above program is executed, it produces the following result ?
a= 10 b = 30.5