C Programming • Structure and Union
C Programming / Unions

Unions

Notes 4 Structure and Union

A union is a user-defined data type in C that allows different members to share the same memory location. A union can contain members of different data types, but at a particular time, the same memory area is used by all its members.

Notes

A union is a user-defined data type in C that allows different members to share the same memory location. A union can contain members of different data types, but at a particular time, the same memory area is used by all its members.

💡 Example
union Data { int number; float value; char grade; };

Here, number, value, and grade share the same memory area.

Syntax of a Union

A union is declared using the union keyword, followed by the union name and its members inside braces.

union union_name { data_type member1; data_type member2; ... };
💡 Example
union Data { int number; float value; };

Declaring a Union Variable

After defining a union, a union variable is declared using the union type. The variable provides access to the members of the union.

💡 Example
union Data d1;

Here, d1 is a variable of type union Data.

Accessing Union Members

Union members are accessed using the dot operator (.), just like members of a structure.

💡 Example
d1.number d1.value

Important Feature of a Union

The most important feature of a union is that all its members share the same memory location. Therefore, assigning a value to one member can affect the value represented by another member.

⚠️ Example
d1.number = 10; d1.value = 25.5;

After assigning d1.value, the same shared memory is being used for that member. Therefore, a union is normally used when only one of its alternative members needs to hold a meaningful value at a time.

Memory Usage

The size of a union is determined by the size required for its largest member, subject to the compiler's alignment requirements. Since members share storage, a union can use less memory than a structure containing the same members.

💡 Example

If a union contains an int, a float, and a char, its storage is large enough to hold the member requiring the largest amount of storage.

Union vs Structure

Both structures and unions can contain members of different data types, but their memory arrangement is different. A structure gives separate storage to its members, whereas a union shares the same storage among its members.

Structure Union
Each member has separate storage. All members share the same storage.
Multiple members can hold meaningful values at the same time. Normally one member is used to hold the meaningful value at a time.
Size is generally based on the total storage of its members and padding. Size is generally based on the largest member and alignment.
Useful for complete records. Useful when alternative representations share storage.

When is a Union Useful?

A union is useful when a program needs to store one of several possible types of information in the same memory area. It is especially useful when saving memory is important.

💡 Example

A data item may sometimes need to store an integer and at another time a floating-point value. A union allows both alternatives to use the same storage.

Practical Example

Problem Statement

Write a C program to define a union containing an integer and a floating-point member. Store a value in each member one at a time and display the value after each assignment.

Learning Outcomes

  • Define and declare a union.
  • Access union members using the dot operator.
  • Observe that union members share the same memory.

Hint

Create a union named Data with members number and value. Assign a value to one member, display it, and then assign a value to the other member.

Theory

Unlike a structure, a union provides one shared memory area for all its members. When a value is assigned to one member, that member becomes the currently stored value in that shared area.

Program

#include <stdio.h> union Data { int number; float value; }; int main() { union Data d1; d1.number = 10; // display the integer value stored in the union printf("Integer value = %d ", d1.number); d1.value = 25.5; // display the floating-point value stored in the union printf("Float value = %.1f ", d1.value); return 0; }

Expected Output

Integer value = 10

Float value = 25.5

Note

The program stores one value and then uses the same shared memory for another member. Therefore, a union should be used when the members represent alternative forms of data rather than values that must all be stored simultaneously.

Quick Revision

Concept Remember
Union User-defined type whose members share memory.
Keyword union
Member Access Use the dot operator.
Memory Members use the same storage area.
Main Use Useful for alternative data that can share storage.

Important Exam Questions

Short Answer Questions

  1. What is a union in C?
  2. How is a union declared?
  3. How are union members accessed?
  4. How is a union different from a structure?
  5. Why do union members share memory?
  6. What determines the size of a union?

Long Answer Questions

  1. Explain the concept of unions in C with syntax and suitable example.
  2. Differentiate between structures and unions.
  3. Write a C program to demonstrate the working of a union.
  4. Explain the memory allocation of a union.
🎥 Recommended Learning

Watch a beginner-friendly explanation of unions in C programming.

▶ Watch: Union in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for unions will be provided here.

🧠 Mind Map

Use the mind map for quick revision of union, shared memory, member access and structure comparison.