📘 Lesson  ·  Lesson 58

Structure vs Union

Structure vs Union

Structure and Union

💡 Note

Both group different data types. A structure gives each member its own memory; a union shares one memory space for all members.

Comparison

StructureUnion
each member separate memoryall members share memory
all members usable togetheronly one member at a time
larger sizesize of largest member

Example

C
struct S { int a; char b; };   // separate memory
union U { int a; char b; };    // shared memory
printf("%lu %lu", sizeof(struct S), sizeof(union U));
Output:
8 4

Summary

  • Structure = separate memory per member; union = shared memory (one at a time).

Structure और Union

💡 Note

दोनों अलग data types group करते हैं। Structure हर member को अलग memory देता है; union सभी members के लिए एक memory share करता है।

तुलना

StructureUnion
हर member अलग memoryसभी members memory share
सभी members एक साथ usableएक समय एक member
बड़ा sizeसबसे बड़े member का size

Example

C
struct S { int a; char b; };   // अलग memory
union U { int a; char b; };    // shared memory
printf("%lu %lu", sizeof(struct S), sizeof(union U));
Output:
8 4

सारांश

  • Structure = हर member अलग memory; union = shared memory (एक समय एक)।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n