In this article, we provide information about Union in C. A union is a user-defined data type that allows storing different data types in the same memory location. It is similar to a structure, but unlike structures, all members of a union share the same memory.

Union in C

What is a Union in C?

In C programming, a union is a user-defined data type that allows storing different types of data in a single memory location.

Key Points:

  • A union can have multiple members, but all share the same memory.
  • Only one member can be accessed at a time.
  • Memory allocated for a union is equal to the size of the largest member.

Syntax of Union

union union_name {

    DataType member1;

    DataType member2;

    DataType member3;

    …

};

Explanation:

  • union is the keyword to define a union.
  • union_name is the name of the union (e.g., employee, item, data).
  • DataType can be int, char, float, double, etc.
  • member1, member2 are variables inside the union.

Example: Defining a Union

union item {

    int x;

    float y;

    char z;

};

Note: The size of this union will be equal to the size of the largest member.

Declaring Union Variables

Union variables can be declared in two ways:

Method 1: Inside the main() function

#include <stdio.h>

union item {

    int x;

    float y;

    char z;

};

void main() {

    union item p, q;

}

Method 2: At the end of union definition

union item {

    int x;

    float y;

    char z;

} p, q;

Dot (.) operator is used to access union members. If using a union pointer, the arrow (->) operator is used.

Initializing Union Members

You can initialize union members individually:

#include <stdio.h>

union item {

    int x;

    float y;

    char z;

};

void main() {

    union item p;

    p.x = 305;

    p.z = ‘u’;

    p.y = 115.5;

}

Only one member value is valid at a time because they share the same memory.

Accessing Union Members

  1. Using Dot (.) Operator

#include <stdio.h>

union item {

    int x;

    float y;

};

void main() {

    union item p;

    p.x = 110;

    printf(“x = %d\n”, p.x);

    p.y = 115.5;

    printf(“y = %f\n”, p.y);

}

Output:

x = 110

y = 115.500000

  1. Using Union Pointer (->) Operator

#include <stdio.h>

union test {

    int x;

    char y;

};

void main() {

    union test p;

    p.x = 66;

    union test* q = &p; // Pointer to union

    printf(“%d %c”, q->x, q->y);

}

Output:

66 B

The ASCII value of 66 is B.

Advantages of Union

  1. Saves memory as all members share the same memory location.
  2. Useful for storing different types of data using a single variable.
  3. Size of union equals the size of the largest member.
  4. Ideal for embedded systems or memory-efficient programs.

Disadvantages of Union

  1. Can access only one member at a time.
  2. Cannot use multiple members simultaneously.
  3. May cause data loss if multiple members are updated without care.

Union Program in C

#include <stdio.h>

union demo {

    int p;

    float q;

    char ch;

};

int main() {

    union demo st;

    st.p = 100;

    st.q = 300.2;

    st.ch = ‘Z’;

    printf(“%d\n”, st.p);

    printf(“%f\n”, st.q);

    printf(“%c\n”, st.ch);

    return 0;

}

Output:

100

300.200012

Z

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *