🟡 Intermediate  ·  Lesson 28

Unions & Enumerations

Unions और Enumerations

What is a union?

A union is a user-defined data type like a structure, but all members share the same memory location. At a time, normally only one member holds a meaningful value.

Syntax
union Data {
    int i;
    float f;
    char str[20];
};

Memory Sharing Example

C Language
#include <stdio.h>
#include <string.h>

union Data {
    int i;
    float f;
    char str[20];
};

int main() {
    union Data d;

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

    d.f = 25.5;
    printf("f = %.1f\n", d.f);

    strcpy(d.str, "C Language");
    printf("str = %s\n", d.str);

    printf("Size of union = %zu bytes", sizeof(d));
    return 0;
}
i = 10 f = 25.5 str = C Language Size of union = 20 bytes
💡 Why size is 20?

The largest member is char str[20], so the union needs enough memory for that member.

Structure vs Union

FeatureStructureUnion
MemorySeparate memory for all membersShared memory for all members
SizeSum of member sizes plus paddingSize of largest member plus padding
UseStore many values togetherStore one of many possible values
AccessAll members can hold valid valuesOnly last assigned member is reliable

Enumerations

An enum gives names to integer constants. It makes programs more readable.

C Language
#include <stdio.h>

enum Week { SUN, MON, TUE, WED, THU, FRI, SAT };

enum Result { FAIL = 0, PASS = 1 };

int main() {
    enum Week today = WED;
    enum Result r = PASS;

    printf("Today value = %d\n", today);
    printf("Result value = %d", r);
    return 0;
}
Today value = 3 Result value = 1

Summary

  • Union members share same memory
  • Union size depends on its largest member
  • Use union when only one value is needed at a time
  • Enum creates readable names for integer constants
  • Enums improve code clarity in menus, status and options

Union क्या है?

Union structure जैसा user-defined data type है, लेकिन इसके सभी members same memory share करते हैं। एक समय पर सामान्यतः एक ही member की value meaningful होती है।

Syntax
union Data {
    int i;
    float f;
    char name[20];
};

Memory Sharing

C Language
#include <stdio.h>
#include <string.h>

union Data {
    int i;
    float f;
    char name[20];
};

int main() {
    union Data d;
    d.i = 10;
    d.f = 20.5;
    strcpy(d.name, "Amit");

    printf("Name = %s", d.name);
    return 0;
}
⚠️ ध्यान दें

Union में last assigned member की value ही reliable होती है क्योंकि memory same होती है।

Structure vs Union

FeatureStructureUnion
Memoryहर member की अलग memoryसभी members same memory share करते हैं
Sizeलगभग सभी members का totalसबसे बड़े member के बराबर
Useकई values साथ रखनी होंएक समय पर एक value रखनी हो

Enumeration

enum integer constants को readable names देता है।

C Language
enum Week { SUN, MON, TUE, WED, THU, FRI, SAT };
enum Week today = MON;
printf("%d", today);   // 1

सारांश

  • Union में members same memory share करते हैं
  • Union का size largest member पर depend करता है
  • Enum integer constants को names देता है
  • Union memory saving के लिए useful है
  • Enum menu/status values के लिए अच्छा है
← Back to C Tutorial