Union
A union is also a user-defined data type that can contain elements of different types. However, unlike structure, a union stores its members in a shared memory location rather than having separate memory for each member.
Syntax of Union
union union_name { // members . . }
Union members can be accessed using dot operator ( . ) but only one member can store the data at a particular instance in time.
Example of Union
C
// C Program to demonstrate how to use union #include <stdio.h> // union template or declaration union un { int member1; char member2; float member3; }; // driver code int main() { // defining a union variable union un var1; // initializing the union member var1.member1 = 15; printf ( "The value stored in member1 = %d" , var1.member1); return 0; } |
The value stored in member1 = 15
C Cheat Sheet
This C Cheat Sheet provides an overview of both basic and advanced concepts of the C language. Whether you’re a beginner or an experienced programmer, this cheat sheet will help you revise and quickly go through the core principles of the C language.
In this Cheat Sheet, we will delve into the basics of the C language, exploring its fundamental concepts that lay the groundwork for programming. We will cover topics such as variables, data types, and operators, providing you with a solid understanding of the building blocks of C programming.
Contact Us