Unit 9: Structures and Unions in C Programming | B. Sc Year: III

🧭 Introduction

Until now, you have worked with simple data types like int, float, and arrays that store similar types of data. But in real-world programs, we often need to store different types of data together.

For example:

  • Student → roll number (int), name (string), marks (float)

  • Employee → ID (int), salary (float), department (string)

To manage such related but different data types, C provides:

  • Structures

  • Unions

Unit 9 teaches you how to group, manage, and efficiently store complex data, which is extremely useful for real applications and very important for exams.


🔑 Key Concepts (Simple Explanation)


1️⃣ What is a Structure?

A structure is a user-defined data type that allows you to store different types of data under one name.

Exam Definition ✅

A structure is a collection of variables of different data types stored together using a single name.

Example (real-life idea):

Student → {roll, name, marks}

2️⃣ Defining a Structure

Syntax:

struct student {
    int roll;
    char name[30];
    float marks;
};
  • student → structure name

  • roll, name, marks → members

✅ Structure definition does not allocate memory until variables are declared.


3️⃣ Declaring Structure Variables

struct student s1, s2;

Memory is allocated here.

Accessing Members:

s1.roll = 10;
printf("%d", s1.roll);

✅ Use the dot operator (.) to access members.


4️⃣ Structure Initialization

struct student s1 = {1, "Ram", 85.5};

✅ Values must be in the same order as structure members.


5️⃣ Structure Program Example (Exam-Oriented)

Program: Store and Display Student Information

#include <stdio.h>

struct student {
    int roll;
    char name[30];
    float marks;
};

int main() {
    struct student s;

    printf("Enter roll number: ");
    scanf("%d", &s.roll);

    printf("Enter name: ");
    scanf("%s", s.name);

    printf("Enter marks: ");
    scanf("%f", &s.marks);

    printf("\nStudent Details:");
    printf("\nRoll = %d", s.roll);
    printf("\nName = %s", s.name);
    printf("\nMarks = %.2f", s.marks);

    return 0;
}

✅ Frequently asked in exams.


6️⃣ Array of Structures

When storing data of multiple objects, we use an array of structures.

struct student s[50];

✅ Used for class records, employee lists, etc.


7️⃣ Pointer to Structure

A pointer to structure stores the address of a structure variable.

Declaration:

struct student *p;

Access Members:

p = &s;
printf("%d", p->roll);

-> is the arrow operator.


8️⃣ Passing Structure to Functions

Structures can be passed:

  • By value (copy sent)

  • By reference (address sent)

Example (By Reference):

void display(struct student *s) {
    printf("%d %s %.2f", s->roll, s->name, s->marks);
}

9️⃣ Nested Structure

A structure inside another structure is called a nested structure.

struct date {
    int day, month, year;
};

struct student {
    int roll;
    struct date dob;
};

✅ Occasionally asked in theory questions.


🔁 What is a Union?

A union is similar to a structure, but all members share the same memory location.

Definition:

A union stores different data types in the same memory space, one at a time.


🔟 Declaring a Union

union data {
    int i;
    float f;
    char c;
};

✅ Memory size of union = size of largest member.


1️⃣1️⃣ Union Example Program

#include <stdio.h>

union data {
    int i;
    float f;
};

int main() {
    union data d;

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

    d.f = 3.14;
    printf("Float = %.2f\n", d.f);

    return 0;
}

⚠️ Only the last assigned value is valid.


🔍 Difference Between Structure and Union

StructureUnion
Separate memory for each memberShared memory
Larger sizeSmaller size
All members usableOne at a time
SaferMemory efficient

✅ One of the most common exam questions.


1️⃣2️⃣ typedef Keyword

typedef gives a new name to an existing data type.

Example:

typedef struct student {
    int roll;
    float marks;
} STUD;

Now you can write:

STUD s1;

✅ Makes code shorter and easier to read.


📝 Important Exam Questions

  1. Define structure with example

  2. Write a C program using structure

  3. What is union? How is it different from structure?

  4. Explain pointer to structure

  5. What is typedef? Advantages

  6. Explain array of structures

  7. Write short notes on:

    • Nested structure

    • Arrow operator (->)


🎯 Exam Tips / Key Points

  • Structure allocates separate memory

  • Union allocates shared memory

  • Use . for variable, -> for pointer

  • In union, only last stored value is valid

  • Programs using structures are high scoring

  • Draw memory diagrams for clarity


✅ Short Summary

In Unit 9, you learned:

  • Structures and unions

  • Defining and using them

  • Difference between structure and union

  • Array of structures

  • Pointer to structure

  • Use of typedef

These concepts are widely used in files, databases, and real programs, and prepare you for file handling, which usually comes next.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers