Experiment 11 - Student Record Using Structures

Aim

To create a student record management system using structures in C programming. Define a structure to store student information (name, roll number, marks) and perform operations like input, display, and search. This experiment helps students understand how to organize related data using structures and work with arrays of structures.

Through this experiment, students will learn to define structures, create arrays of structures, access structure members, pass structures to functions, and implement basic database-like operations for data management.

Algorithm

The algorithm for student record management involves structure operations:

Algorithm Steps:

  1. Define Structure: Create Student structure with members (rollNumber, name, marks[], total, percentage)
  2. Declare Array: Create array of Student structures to store multiple records
  3. Input Records: For each student
    • Read rollNumber, name
    • Read marks for each subject
    • Calculate total = sum of all marks
    • Calculate percentage = (total / (subjects × 100)) × 100
  4. Display Records: Loop through array, display each student's information
  5. Search Operation:
    • Read search roll number
    • Loop through array comparing rollNumber
    • If found, display student details; if not, show "not found"

Procedure

  1. Include stdio.h and string.h header files
  2. Define a structure Student with members:
    • rollNumber (int)
    • name (char array)
    • marks (float array for multiple subjects or single float)
    • total (float)
    • percentage (float)
  3. Declare an array of structures to store multiple students
  4. Create functions for:
    • Input student data
    • Display student information
    • Calculate total and percentage
    • Search student by roll number
  5. Use dot operator (.) to access structure members
  6. Use arrow operator (->) when working with structure pointers

Flowchart

START
  |
  +-> Define structure Student
  |   { rollNumber, name, marks[], total, percentage }
  |
  +-> Declare: students[], n, i
  |
  +-> Read n (number of students)
  |
  +-> FOR i = 0 to n-1
  |   |
  |   +-> Read students[i].rollNumber
  |   +-> Read students[i].name
  |   +-> Read students[i].marks (for each subject)
  |   +-> Calculate students[i].total
  |   +-> Calculate students[i].percentage
  |
  +-> Display all student records
  |
  +-> Read searchRollNumber
  |
  +-> FOR i = 0 to n-1
  |   |
  |   +-> IF (students[i].rollNumber == searchRollNumber)
  |   |   |
  |   |   +-> Display student[i] details
  |   |   +-> BREAK
  |   |
  |   +-> END FOR
  |
  +-> END

Program Code

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

#define MAX_SUBJECTS 5

struct Student {
    int rollNumber;
    char name[50];
    float marks[MAX_SUBJECTS];
    float total;
    float percentage;
};

void inputStudent(struct Student *s, int index) {
    int i;
    printf("\n--- Enter details for Student %d ---\n", index + 1);
    
    printf("Roll Number: ");
    scanf("%d", &s->rollNumber);
    
    printf("Name: ");
    scanf(" %[^\n]", s->name);
    
    printf("Enter marks for %d subjects:\n", MAX_SUBJECTS);
    s->total = 0;
    for (i = 0; i < MAX_SUBJECTS; i++) {
        printf("Subject %d: ", i + 1);
        scanf("%f", &s->marks[i]);
        s->total += s->marks[i];
    }
    
    s->percentage = (s->total / (MAX_SUBJECTS * 100)) * 100;
}

void displayStudent(struct Student s) {
    int i;
    printf("\n--- Student Details ---\n");
    printf("Roll Number: %d\n", s.rollNumber);
    printf("Name: %s\n", s.name);
    printf("Marks: ");
    for (i = 0; i < MAX_SUBJECTS; i++) {
        printf("%.2f ", s.marks[i]);
    }
    printf("\n");
    printf("Total: %.2f\n", s.total);
    printf("Percentage: %.2f%%\n", s.percentage);
}

int searchStudent(struct Student students[], int n, int rollNumber) {
    int i;
    for (i = 0; i < n; i++) {
        if (students[i].rollNumber == rollNumber) {
            return i;
        }
    }
    return -1;
}

int main() {
    struct Student students[100];
    int n, i, searchRoll, index;
    
    printf("Enter number of students: ");
    scanf("%d", &n);
    
    for (i = 0; i < n; i++) {
        inputStudent(&students[i], i);
    }
    
    printf("\n========== ALL STUDENT RECORDS ==========\n");
    for (i = 0; i < n; i++) {
        displayStudent(students[i]);
    }
    
    printf("\nEnter roll number to search: ");
    scanf("%d", &searchRoll);
    
    index = searchStudent(students, n, searchRoll);
    if (index != -1) {
        printf("\nStudent found!\n");
        displayStudent(students[index]);
    } else {
        printf("\nStudent with roll number %d not found.\n", searchRoll);
    }
    
    return 0;
}

Sample Input and Output

Sample 1:

Input:

Enter number of students: 2

--- Enter details for Student 1 ---
Roll Number: 101
Name: John Doe
Enter marks for 5 subjects:
Subject 1: 85
Subject 2: 90
Subject 3: 78
Subject 4: 92
Subject 5: 88

--- Enter details for Student 2 ---
Roll Number: 102
Name: Jane Smith
Enter marks for 5 subjects:
Subject 1: 95
Subject 2: 87
Subject 3: 91
Subject 4: 89
Subject 5: 93

Enter roll number to search: 101

Output:

========== ALL STUDENT RECORDS ==========

--- Student Details ---
Roll Number: 101
Name: John Doe
Marks: 85.00 90.00 78.00 92.00 88.00 
Total: 433.00
Percentage: 86.60%

--- Student Details ---
Roll Number: 102
Name: Jane Smith
Marks: 95.00 87.00 91.00 89.00 93.00 
Total: 455.00
Percentage: 91.00%

Student found!

--- Student Details ---
Roll Number: 101
Name: John Doe
Marks: 85.00 90.00 78.00 92.00 88.00 
Total: 433.00
Percentage: 86.60%

Use Case / Real-world Relevance

Structures are essential for organizing related data and are used extensively:

  • Database Systems: Representing records in databases, employee records, customer data
  • Information Systems: Student management, library systems, inventory management
  • Game Development: Player profiles, game objects, character attributes
  • System Programming: File metadata, process information, system structures
  • Data Structures: Building complex data structures like linked lists, trees, graphs

Viva Questions

Q1: What is the difference between dot (.) and arrow (->) operators?

Dot operator (.) is used to access structure members when you have a structure variable: student.name. Arrow operator (->) is used when you have a pointer to structure: ptr->name is equivalent to (*ptr).name. Arrow is shorthand for dereference then access member.

Q2: Why do we use an array of structures instead of separate arrays?

Array of structures keeps related data together, ensuring data integrity (all fields for one student stay together), easier to pass to functions, and more intuitive organization. Separate arrays (names[], rollNos[], marks[][]) require maintaining parallel indices and are error-prone. Structures model real-world entities better.

Q3: Should we pass structures to functions by value or by reference?

Pass by reference (pointer) for large structures to avoid copying overhead. Pass by value creates a copy of entire structure, which is inefficient for large structures. Use pointers when you need to modify the structure or for efficiency. Pass by value only for small structures or when you need a copy.

Q4: How do we initialize a structure array?

Can initialize at declaration: struct Student s = {101, "John", {85, 90, 78}, 253, 84.33};. For arrays: struct Student arr[] = {{101, "John", ...}, {102, "Jane", ...}};. Or initialize element by element using loops and input functions. Designated initializers (C99) allow:{.rollNumber = 101, .name = "John"}.

Q5: Can we have nested structures (structure within structure)?

Yes, structures can contain other structures as members. For example, Student structure could contain Date structure for date of birth. Access nested members using multiple dots: student.dob.day, student.dob.month, student.dob.year. This allows modeling complex hierarchical data.

🔹 Author: Dr. J. Siva Ramakrishna

🔹 Institution: Narayana Engineering College, Gudur

🔹 Last Updated: 9 January 2026