Library Management System Project in C with Source Code

In this blog post, we will explore a simple library management system written in the C language. 

Features of the Library Management System: 

Book Management: Add, view, update, or delete books. 

Member Management: Register a new member, update member details, or delete a member. 

Issue & Return Books: Members can borrow and return books. 

Implementation

1. Data Structures: 

Firstly, let's define our core data structures - Book and Member.

typedef struct {
    int id;
    char title[50];
    char author[50];
    int isAvailable;
} Book;

typedef struct {
    int memberId;
    char memberName[50];
    Book borrowedBooks[5];
} Member;

2. Global Data: 

For simplicity, we'll use arrays to store books and members.

Book books[100];
int bookCount = 0;

Member members[100];
int memberCount = 0;

3. Functions: 

Here are the functions to implement our features:

// Add a new book
void addBook() {
    Book newBook;
    printf("Enter Book ID: ");
    scanf("%d", &newBook.id);
    printf("Enter Book Title: ");
    scanf("%s", newBook.title);
    printf("Enter Book Author: ");
    scanf("%s", newBook.author);
    newBook.isAvailable = 1; // Initially, every book is available
    books[bookCount++] = newBook;
}

// Register a new member
void registerMember() {
    Member newMember;
    printf("Enter Member ID: ");
    scanf("%d", &newMember.memberId);
    printf("Enter Member Name: ");
    scanf("%s", newMember.memberName);
    for (int i = 0; i < 5; i++) {
        newMember.borrowedBooks[i].id = -1; // No books borrowed initially
    }
    members[memberCount++] = newMember;
}

// Borrow a book
void borrowBook(int memberId, int bookId) {
    for (int i = 0; i < bookCount; i++) {
        if (books[i].id == bookId && books[i].isAvailable) {
            for (int j = 0; j < memberCount; j++) {
                if (members[j].memberId == memberId) {
                    for (int k = 0; k < 5; k++) {
                        if (members[j].borrowedBooks[k].id == -1) {
                            members[j].borrowedBooks[k] = books[i];
                            books[i].isAvailable = 0;
                            return;
                        }
                    }
                }
            }
        }
    }
}

// Return a book
void returnBook(int memberId, int bookId) {
    for (int i = 0; i < memberCount; i++) {
        if (members[i].memberId == memberId) {
            for (int j = 0; j < 5; j++) {
                if (members[i].borrowedBooks[j].id == bookId) {
                    members[i].borrowedBooks[j].id = -1;
                    for (int k = 0; k < bookCount; k++) {
                        if (books[k].id == bookId) {
                            books[k].isAvailable = 1;
                            return;
                        }
                    }
                }
            }
        }
    }
}

4. Main Function: 

Here, we'll provide a simple menu for interaction:

int main() {
    int choice;
    do {
        printf("\n1. Add Book\n2. Register Member\n3. Borrow Book\n4. Return Book\n5. Exit\nEnter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: addBook(); break;
            case 2: registerMember(); break;
            case 3: {
                int memberId, bookId;
                printf("Enter Member ID: ");
                scanf("%d", &memberId);
                printf("Enter Book ID: ");
                scanf("%d", &bookId);
                borrowBook(memberId, bookId);
                break;
            }
            case 4: {
                int memberId, bookId;
                printf("Enter Member ID: ");
                scanf("%d", &memberId);
                printf("Enter Book ID: ");
                scanf("%d", &bookId);
                returnBook(memberId, bookId);
                break;
            }
            case 5: printf("Goodbye!\n"); break;
            default: printf("Invalid choice!\n"); break;
        }
    } while (choice != 5);

    return 0;
}

Complete Source Code with Output

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

typedef struct {
    int id;
    char title[50];
    char author[50];
    int isAvailable;
} Book;

typedef struct {
    int memberId;
    char memberName[50];
    Book borrowedBooks[5];
} Member;

Book books[100];
int bookCount = 0;

Member members[100];
int memberCount = 0;

void addBook() {
    Book newBook;
    printf("Enter Book ID: ");
    scanf("%d", &newBook.id);
    getchar(); // Consume newline left by scanf
    printf("Enter Book Title: ");
    fgets(newBook.title, 50, stdin);
    strtok(newBook.title, "\n"); // Remove newline character
    printf("Enter Book Author: ");
    fgets(newBook.author, 50, stdin);
    strtok(newBook.author, "\n"); // Remove newline character
    newBook.isAvailable = 1;
    books[bookCount++] = newBook;
}

void registerMember() {
    Member newMember;
    printf("Enter Member ID: ");
    scanf("%d", &newMember.memberId);
    getchar(); // Consume newline left by scanf
    printf("Enter Member Name: ");
    fgets(newMember.memberName, 50, stdin);
    strtok(newMember.memberName, "\n"); // Remove newline character
    for (int i = 0; i < 5; i++) {
        newMember.borrowedBooks[i].id = -1;
    }
    members[memberCount++] = newMember;
}

void borrowBook(int memberId, int bookId) {
    for (int i = 0; i < bookCount; i++) {
        if (books[i].id == bookId && books[i].isAvailable) {
            for (int j = 0; j < memberCount; j++) {
                if (members[j].memberId == memberId) {
                    for (int k = 0; k < 5; k++) {
                        if (members[j].borrowedBooks[k].id == -1) {
                            members[j].borrowedBooks[k] = books[i];
                            books[i].isAvailable = 0;
                            printf("Book borrowed successfully!\n");
                            return;
                        }
                    }
                }
            }
        }
    }
    printf("Cannot borrow the book.\n");
}

void returnBook(int memberId, int bookId) {
    for (int i = 0; i < memberCount; i++) {
        if (members[i].memberId == memberId) {
            for (int j = 0; j < 5; j++) {
                if (members[i].borrowedBooks[j].id == bookId) {
                    members[i].borrowedBooks[j].id = -1;
                    for (int k = 0; k < bookCount; k++) {
                        if (books[k].id == bookId) {
                            books[k].isAvailable = 1;
                            printf("Book returned successfully!\n");
                            return;
                        }
                    }
                }
            }
        }
    }
    printf("Cannot return the book.\n");
}

int main() {
    int choice;
    do {
        printf("\n1. Add Book\n2. Register Member\n3. Borrow Book\n4. Return Book\n5. Exit\nEnter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: addBook(); break;
            case 2: registerMember(); break;
            case 3: {
                int memberId, bookId;
                printf("Enter Member ID: ");
                scanf("%d", &memberId);
                printf("Enter Book ID: ");
                scanf("%d", &bookId);
                borrowBook(memberId, bookId);
                break;
            }
            case 4: {
                int memberId, bookId;
                printf("Enter Member ID: ");
                scanf("%d", &memberId);
                printf("Enter Book ID: ");
                scanf("%d", &bookId);
                returnBook(memberId, bookId);
                break;
            }
            case 5: printf("Goodbye!\n"); break;
            default: printf("Invalid choice!\n"); break;
        }
    } while (choice != 5);

    return 0;
}
Output:
1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 1
Enter Book ID: 100
Enter Book Title: Learn C language
Enter Book Author: John
1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 2
Enter Member ID: 1
Enter Member Name: Tom
1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 3
Enter Member ID: 1
Enter Book ID: 100
Book borrowed successfully!

1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 4
Enter Member ID: 1
Enter Book ID: 100
Book returned successfully!

1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 3
Enter Member ID: 1
Enter Book ID: 100
Book borrowed successfully!

1. Add Book
2. Register Member
3. Borrow Book
4. Return Book
5. Exit
Enter choice: 5
Goodbye!

Conclusion

The code provided above offers a simple and foundational approach to a library management system using C. It is a console-based program and doesn't use any form of persistent storage like databases. This project serves as an excellent starting point for beginners looking to practice their C programming skills. You can enhance this project by integrating file operations for data storage, refining the user interface, or introducing additional functionalities like handling book genres, tracking late returns, and so on. 

Happy coding!


Comments