🔗 Union in C Language

🔹 What is a Union?

A union in C is a user-defined data type, similar to a structure, but all members share the same memory location. Only one member can hold a value at a time.

Key Idea:
✔ Memory-efficient alternative to structures
✔ All members share the same address

Article Algo

✅ Why Use Union?

  • Saves memory by sharing memory among members
  • Useful when only one value is needed at a time
  • Common in embedded systems and low-memory programs

📌 Syntax of Union

union union_name {
  data_type member1;
  data_type member2;
  ...
};

📌 Declaring Union Variables

union Data {
  int i;
  float f;
  char c;
};

union Data d1;

📌 Accessing Union Members

d1.i = 10;
d1.f = 3.5;  // i value is overwritten
printf("%f", d1.f);

Note: Only the last assigned member contains a valid value.

📘 Example Program: Union in C

#include <stdio.h>

union Data {
  int i;
  float f;
  char c;
};

int main() {
  union Data d;

  d.i = 10;
  printf("i: %d\n", d.i);

  d.f = 3.5;
  printf("f: %.2f\n", d.f);
  printf("i (overwritten): %d\n", d.i);

  d.c = 'A';
  printf("c: %c\n", d.c);

  return 0;
}

🧠 Memory Allocation in Union

  • Memory allocated = size of the largest member
  • All members share the same memory location
Member Size Address
int i 4 1000
float f 4 1000
char c 1 1000

👍 Advantages of Union

  • Efficient memory usage
  • Useful for storing different types at different times
  • Reduces program memory footprint

👎 Disadvantages of Union

  • Only one member can store value at a time
  • Overwriting may cause data loss
  • Cannot store multiple values simultaneously

📂 Types of Union in C

1️⃣ Simple Union

union Data {
  int i;
  float f;
  char c;
};

2️⃣ Array of Unions

#include <stdio.h>

union Data {
  int i;
  float f;
};

int main() {
  union Data arr[2];
  arr[0].i = 10;
  arr[1].f = 5.5;

  printf("%d %.2f", arr[0].i, arr[1].f);
  return 0;
}

3️⃣ Pointer to Union

#include <stdio.h>

union Data {
  int i;
  float f;
};

int main() {
  union Data d;
  union Data *ptr = &d;

  ptr->i = 100;
  printf("%d", ptr->i);
  return 0;
}

4️⃣ Nested Union

#include <stdio.h>

union Inner {
  int i;
  float f;
};

struct Outer {
  char name[20];
  union Inner u;
};

int main() {
  struct Outer o;
  o.u.i = 50;
  printf("%d", o.u.i);
  return 0;
}

5️⃣ Union with Functions

#include <stdio.h>

union Data {
  int i;
  float f;
};

void display(union Data *d) {
  printf("%d\n", d->i);
}

int main() {
  union Data d1;
  d1.i = 25;
  display(&d1);
  return 0;
}

⚖️ Difference Between Union and Structure

Feature Union Structure
Memory Shared by all members Separate for each member
Size Size of largest member Sum of all members
Value Storage Only one at a time All members
Use Memory efficiency Multiple values together

📚 FAQs on Union in C

Q1. What is a union?

A union is a user-defined data type where all members share the same memory.

Q2. Can a union hold multiple values?

No, only one member can hold a value at a time.

Q3. What is the size of a union?

It is equal to the size of the largest member.

Q4. Can we have pointers to unions?

Yes, using the -> operator.

Q5. When should we use a union?

When memory efficiency is important.

🔑 Key Points to Remember

  • Unions are memory-efficient
  • Only one member holds value at a time
  • Use . and -> operators
  • Useful in embedded and low-memory systems