C Programming
About Lesson
1: Introduction to Unions

In C, a union is a special data type that allows storing different data types in the same memory location. Unlike structures, which allocate separate memory for each member, unions share the same memory space for all members.

  • Union Definition Syntax:

    union Data {
    int intValue;
    float floatValue;
    char stringValue[20];
    };
  • Union Declaration and Initialization:

    union Data data;
    data.intValue = 10;
 2: Differences Between Structures and Unions

Structures (struct) and unions (union) have key differences in how they allocate memory and access their members:

  • Memory Allocation:

    • Structures allocate separate memory for each member, resulting in a memory size equal to the sum of its members’ sizes.
    • Unions allocate memory that is large enough to hold its largest member, sharing the same memory space among all members.
  • Member Access:

    • All members of a structure can be accessed simultaneously.
    • Only one member of a union can be accessed at a time, sharing the same memory space.
3: Applications of Unions

Unions are useful for scenarios where you want to store different types of data in the same memory location, particularly when only one type of data is used at a time. Common applications of unions include:

  • Memory Optimization:

    • Unions help save memory by allowing multiple data types to share the same memory space.
  • Handling Variant Data:

    • Unions are used in situations where different data types can be used interchangeably based on specific conditions.
  • Implementing Variant Records:

    • Unions can be used to represent variant records, where different fields of a record are valid in different situations.
4: Practice and Examples
  • Practice Exercise:
    • Write a program that uses a union to store different types of values (int, float, char) and demonstrates accessing its members.
    • Implement a function that uses a union to handle a variant data type (e.g., representing a numerical value that could be an integer or a floating-point number based on user input).
#include <stdio.h>

// Union definition to store different types of values
union Variant {
int intValue;
float floatValue;
char stringValue[20];
};

int main() {
union Variant var;

// Assigning values to union members
var.intValue = 10;
printf("Integer value: %dn", var.intValue);

var.floatValue = 3.14;
printf("Float value: %.2fn", var.floatValue);

strcpy(var.stringValue, "Hello");
printf("String value: %sn", var.stringValue);

// Accessing union members after reassignment
var.intValue = 20;
printf("Updated integer value: %dn", var.intValue);

return 0;
}

Unions provide flexibility in storing and accessing different data types within the same memory location. Practice using unions to handle variant data efficiently and understand their role in memory optimization and data representation. Experiment with different scenarios where unions can be applied effectively to solve specific programming challenges. Understanding unions complements your knowledge of data structures and enhances your ability to design memory-efficient programs in C.