In real-life problems, we often need to store different kinds of information together β€” for example, a student's name (string), roll number (integer), and marks (float). C provides a feature called a Structure to group such related data items of different types under one name. Similarly, Unions are another feature that help save memory when only one of the members is used at a time.

Types of Operators in C

A Structure is a user-defined data type that allows you to combine variables of different types. It’s useful for representing complex data like students, employees, or products.

struct structure_name {
	data_type member1;
	data_type member2;
	...
};

Example:

#include <stdio.h>
struct Student {
    int roll;
    char name[30];
    float marks;
};

int main() {
    struct Student s1 = {101, "Ravi", 89.5};
    printf("Roll: %d\\n", s1.roll);
    printf("Name: %s\\n", s1.name);
    printf("Marks: %.2f\\n", s1.marks);

    return 0;
}

  1. Accessing Structure Members
  2. Use the . (dot operator) to access individual members of a structure.

    s1.roll = 102;
    strcpy(s1.name, "Asha");
    s1.marks = 92.3;
    
    printf("%s scored %.1f marks", s1.name, s1.marks);

  3. Array of Structures
  4. We can create multiple records of the same type using arrays of structures.

    
    struct Student s[3] = {
    	{1, "Ravi", 88.5},
    	{2, "Meena", 91.0},
    	{3, "Ajay", 79.5}
    	};
    
    	for(int i = 0; i < 3; i++)
    	printf("%d %s %.1f\\n", s[i].roll, s[i].name, s[i].marks);
    

  5. Structure within Structure (Nested Structure)
  6. struct Date {
        int day, month, year;
    };
    
    struct Student {
        int roll;
        char name[30];
        struct Date dob; // nested structure
    };
    
    struct Student s1 = {101, "Ravi", {12, 5, 2004}};
    printf("DOB: %d-%d-%d", s1.dob.day, s1.dob.month, s1.dob.year);

  7. Passing Structures to Functions
  8. void display(struct Student s) {
        printf("%d %s %.1f\\n", s.roll, s.name, s.marks);
    }
    int main() {
        struct Student s1 = {101, "Ravi", 88.5};
        display(s1);
    }
    Note: You can pass structures by value or by reference (using pointers).

  9. Pointers to Structures
  10. struct Student s1 = {101, "Ravi", 88.5};
    struct Student *ptr = &s1;
    
    printf("%s", ptr->name);   // use arrow operator (->)
    
Tip: Use ptr->member instead of (*ptr).member for easy access.

A Union is similar to a structure, but it shares the same memory location for all members. Only one member can contain a value at a time β€” making it memory-efficient.

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

int main() {
    union Data d1;

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

    d1.f = 22.5;
    printf("f = %.1f\\n", d1.f);

    strcpy(d1.str, "Hello");
    printf("str = %s\\n", d1.str);

    return 0;
}

Difference Between Structure and Union

Feature Structure Union
MemorySeparate memory for each memberShared memory for all members
UsageAll members can store values simultaneouslyOnly one member holds a value at a time
SizeSum of all member sizesSize of the largest member
ExampleStudent info (roll, marks)Value that may change type (int/float/string)

Summary

  • Structures group variables of different types together.
  • Use . and -> operators to access members.
  • Unions save memory by sharing space between members.
  • Structures are used for data records; Unions are for type-flexible data storage.
In short: Structures organize data neatly, while Unions help when you need to store different types in the same space β€” both make C more powerful and efficient!