Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Structures vs. Unions in C: A Practical Guide

What is a Structure?

A structure is a user‑defined data type in C that groups logically related data items of different types into a single unit. Each member occupies its own contiguous memory location, so a structure can hold multiple values simultaneously.

Example: a student record that stores a name, roll number, and marks under one variable name.

What is a Union?

A union is also a user‑defined type, but all members share the same memory space. The union’s size equals the size of its largest member. This feature makes unions ideal for memory‑constrained scenarios where only one member is needed at a time.

Declaring a Structure

struct Student {
    char name[60];
    int  roll_no;
    float marks;
};

Notice the use of the struct keyword and the semicolon after the closing brace.

Example: Using a Structure

#include <stdio.h>

struct Student {
    char name[60];
    int  roll_no;
    float marks;
} sdt;

int main(void) {
    printf("Enter the following information:\n");
    printf("Student name: ");
    fgets(sdt.name, sizeof(sdt.name), stdin);

    printf("Roll number: ");
    scanf("%d", &sdt.roll_no);

    printf("Marks: ");
    scanf("%f", &sdt.marks);

    printf("\nEntered data:\n");
    printf("Name: %s", sdt.name);
    printf("Roll number: %d\n", sdt.roll_no);
    printf("Marks: %.1f\n", sdt.marks);
    return 0;
}

Output (example):

Enter the following information:
Student name: James
Roll number: 21
Marks: 67

Entered data:
Name: James
Roll number: 21
Marks: 67.0

Declaring a Union

union Item {
    int    x;
    float  y;
    char   ch;
};

Example: Using a Union

#include <stdio.h>

union Item {
    int    x;
    float  y;
    char   ch;
};

int main(void) {
    union Item it;
    it.x = 12;
    it.y = 20.2f;
    it.ch = 'a';

    printf("x = %d\n", it.x);
    printf("y = %f\n", it.y);
    printf("ch = %c\n", it.ch);
    return 0;
}

Because all members share memory, setting ch overwrites the previous values of x and y. Only the last stored value is valid.

Structure vs. Union

Structures vs. Unions in C: A Practical Guide

FeatureStructureUnion
Declaration keywordstructunion
Memory layoutEach member gets its own spaceAll members share one space
Changing one memberDoes not affect othersOverwrites other members
InitializationAll members can be initialized simultaneouslyOnly the first member can be initialized at a time
Total sizeSum of all member sizesSize of the largest member
Typical useStore related data of various typesStore one of several possible types
Access patternAny member can be accessed at any timeOnly one member is valid at a time
Flexible array supportSupportedNot supported

When to Use Each

Pros and Cons

Structure Advantages

Structure Disadvantages

Union Advantages

Union Disadvantages

C Language

  1. C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
  2. C vs. C++: Key Differences & When to Choose Each
  3. C vs Java: A Comprehensive Comparison of Features, History, and Applications
  4. Python vs JavaScript: Key Differences, Features, and When to Choose Each
  5. Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases
  6. Joining vs Welding: Key Differences Explained
  7. Welding vs Brazing: Key Differences Explained for Better Metal Joining
  8. Up vs Down Milling: Key Differences Explained
  9. Soldering vs. Brazing: Understanding the Critical Differences
  10. Key Differences Between Ships and Boats Explained