Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C Unions Explained: Definition, Usage, and Comparison with Structures

C Unions Explained

Learn how to define, create, and access C unions, and discover why they differ from structures in memory layout and application.

A union is a user‑defined type in C that, unlike a struct, can hold only one of its member values at a time. This makes unions ideal for memory‑efficient data representations and type‑punning in low‑level programming.


Defining a Union

Use the union keyword to declare a new type. For example:

union Car {
  char name[50];
  int  price;
};

The above code creates a derived type union Car that can store either a 50‑character string or an integer price.


Creating Union Variables

Defining a union does not allocate memory. To use it, declare variables of that type:

union Car car1, car2, *car3;

Alternatively, you can combine the definition and variable declaration:

union Car { char name[50]; int price; } car1, car2, *car3;

In both examples, car1 and car2 are union instances, while car3 is a pointer to a union.


Accessing Union Members

Use the dot (.) operator for direct access and the arrow (->) for pointer access:


Unions vs. Structures

While structs allocate enough space for all members, a union’s size equals the size of its largest member. Consider this comparison:

#include <stdio.h>

union UnionJob {
   char  name[32];
   float salary;
   int   workerNo;
} uJob;

struct StructJob {
   char  name[32];
   float salary;
   int   workerNo;
} sJob;

int main() {
   printf("size of union = %zu bytes\n", sizeof(uJob));
   printf("size of structure = %zu bytes", sizeof(sJob));
   return 0;
}

Output:

size of union = 32
size of structure = 40

Here, the struct occupies 40 bytes (32 + 4 + 4), while the union occupies only 32 bytes – the size of its largest member, name[32]. All union members share the same memory region.


Practical Example: Member Overwrite

#include <stdio.h>

union Job {
   float salary;
   int   workerNo;
} j;

int main() {
   j.salary = 12.3;
   j.workerNo = 100;  // overwrites the previous value
   printf("Salary = %.1f\n", j.salary);
   printf("Number of workers = %d", j.workerNo);
   return 0;
}

Output:

Salary = 0.0
Number of workers = 100

Because salary and workerNo share the same memory, writing to one clears the other.


To explore real‑world use cases, read Why do we need C Unions?.

C Language

  1. Understanding Conductor Size: Wire Gauges, Cross‑Sectional Area, and Practical Applications
  2. Master Java Enums: A Complete Guide to Enums & Enum Classes
  3. Java Enum Constructors Explained with Practical Example
  4. Java EnumSet: Creation, Operations, and Performance Tips
  5. Renesas Launches Ultra‑Compact 8.2‑mm Creepage Photocouplers to Shrink Industrial Automation and Solar Inverter Designs
  6. Structures vs. Unions in C: A Practical Guide
  7. C Unions Explained: Efficient Memory Management for Multiple Data Types
  8. Precise Cable Size Calculation for LT & HT Motors: Safety, Efficiency, and Cost Savings
  9. Mastering Pneumatic Valve Sizing: A Practical Guide
  10. Understanding Pipe Unions: Definition, Use, and Benefits