Creating a full-fledged Library Management System involves multiple components and can be quite extensive. Here, I’ll provide you with a simple console-based implementation in C that focuses on managing books and patrons.
Thank you for reading this post, don't forget to subscribe!For simplicity, we won’t cover transactions like borrowing and returning books, but you can extend the functionality based on your requirements.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define structures for Book and Patron
struct Book {
int bookId;
char title[100];
char author[100];
};
struct Patron {
int patronId;
char name[100];
};
// Function to add a book to the library
void addBook(struct Book books[], int* bookCount) {
printf("\nEnter Book Details:\n");
struct Book newBook;
newBook.bookId = *bookCount + 1;
printf("Title: ");
scanf("%s", newBook.title);
printf("Author: ");
scanf("%s", newBook.author);
books[*bookCount] = newBook;
(*bookCount)++;
printf("\nBook Added Successfully!\n");
}
// Function to display all books in the library
void displayBooks(struct Book books[], int bookCount) {
printf("\nLibrary Books:\n");
for (int i = 0; i < bookCount; i++) {
printf("Book ID: %d, Title: %s, Author: %s\n", books[i].bookId, books[i].title, books[i].author);
}
}
// Function to add a patron to the library
void addPatron(struct Patron patrons[], int* patronCount) {
printf("\nEnter Patron Details:\n");
struct Patron newPatron;
newPatron.patronId = *patronCount + 1;
printf("Name: ");
scanf("%s", newPatron.name);
patrons[*patronCount] = newPatron;
(*patronCount)++;
printf("\nPatron Added Successfully!\n");
}
// Function to display all patrons in the library
void displayPatrons(struct Patron patrons[], int patronCount) {
printf("\nLibrary Patrons:\n");
for (int i = 0; i < patronCount; i++) {
printf("Patron ID: %d, Name: %s\n", patrons[i].patronId, patrons[i].name);
}
}
int main() {
struct Book libraryBooks[100];
struct Patron libraryPatrons[100];
int bookCount = 0;
int patronCount = 0;
int choice;
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Display Books\n");
printf("3. Add Patron\n");
printf("4. Display Patrons\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook(libraryBooks, &bookCount);
break;
case 2:
displayBooks(libraryBooks, bookCount);
break;
case 3:
addPatron(libraryPatrons, &patronCount);
break;
case 4:
displayPatrons(libraryPatrons, patronCount);
break;
case 0:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid Choice. Please try again.\n");
}
} while (choice ! = 0);
return 0;
}
This is a simple console-based application. You can expand on this by adding more features like book transactions (borrowing, returning), storing data persistently (e.g., in files), and implementing a more robust user interface.