A real-life example in C could be a program for managing a student database. Let's break down the components and operations of such a program:
Student Structure: You can define a structure to represent a student, which includes fields like name, roll number, age, grade, etc.
Add Student Function: This function would allow the user to input information about a new student and add it to the database.
Display Student Function: This function would display all the students currently in the database.
Search Student Function: This function would allow the user to search for a student by their roll number or name and display their information if found.
Modify Student Function: This function would allow the user to modify the information of a particular student.
Delete Student Function: This function would allow the user to remove a student from the database.
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
// Define structure for student
struct Student {
char name[50];
int rollNumber;
int age;
char grade;
};
// Global array to store student records
struct Student students[MAX_STUDENTS];
int numStudents = 0; // Counter for number of students
// Function to add a new student
void addStudent() {
if (numStudents < MAX_STUDENTS) {
struct Student newStudent;
printf("Enter name: ");
scanf("%s", newStudent.name);
printf("Enter roll number: ");
scanf("%d", &newStudent.rollNumber);
printf("Enter age: ");
scanf("%d", &newStudent.age);
printf("Enter grade: ");
scanf(" %c", &newStudent.grade);
students[numStudents++] = newStudent;
printf("Student added successfully.\n");
} else {
printf("Maximum number of students reached.\n");
}
}
// Function to display all students
void displayStudents() {
if (numStudents == 0) {
printf("No students in the database.\n");
} else {
printf("Student Database:\n");
for (int i = 0; i < numStudents; i++) {
printf("Name: %s, Roll Number: %d, Age: %d, Grade: %c\n",
students[i].name, students[i].rollNumber,
students[i].age, students[i].grade);
}
}
}
int main() {
int choice;
do {
printf("\nStudent Database Management System\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
In this example
Student
structure to represent a student's information.addStudent()
to add a new student, displayStudents()
to display all students, and a simple menu-driven interface in the main()
function to interact with the user.students
to store student records and numStudents
to keep track of the number of students currently in the database.This program emulates a simple student database management system where users can add new students and display all students in the database. It's a basic example of how C can be used for real-life applications like data management.