Skip to Content
Course content

226: Building a Basic Library Management System

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I want to show you something that almost every C developer trips over when they start building a system that handles text, like a library catalog. You've probably got your structs set up and your array of books ready to go, but when you try to search for a book, your program insists it doesn't exist—even though you can see it right there in the array.

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

// ... inside a search function ...
char searchTitle[100];
scanf("%s", searchTitle);

for (int i = 0; i < bookCount; i++) {
    if (books[i].title == searchTitle) { // The "Invisible" Bug
        printf("Found it! ID: %d\n", books[i].id);
        return;
    }
}
printf("Book not found.\n");

Why your search is returning nothing

At first glance, that if statement looks like perfect English: "if the book's title is equal to the search title." But in C, books[i].title and searchTitle are both arrays of characters. When you use the == operator on arrays, you aren't comparing the letters inside them. You're comparing their memory addresses.

Since books[i].title lives in one part of your memory and searchTitle lives in another, they will never be equal. The contents might be identical—both might say "The Great Gatsby"—but their addresses are different. This is why your loop finishes without ever finding a match.

Swapping pointers for string comparisons

To actually compare the text, you need to use strcmp() from the <string.h> library. It iterates through the strings character by character until it finds a difference or hits the null terminator. The tricky part is that strcmp returns 0 if the strings are identical, which is the opposite of what you'd expect from a boolean check.

if (strcmp(books[i].title, searchTitle) == 0) {
    printf("Found it! ID: %d\n", books[i].id);
    return;
}

I highly recommend getting used to this pattern now. Whether you're building a library system or a game engine, string comparison in C is never as simple as a single operator.

Designing the Book structure

For a basic management system, we need a way to keep track of the state of the book—specifically, whether it's currently checked out. I usually prefer a simple int acting as a boolean for this. It's clean and easy to flip.

typedef struct {
    int id;
    char title[100];
    char author[100];
    int isCheckedOut; // 0 for available, 1 for borrowed
} Book;

You'll want to store these in a fixed-size array for a basic version, or a dynamically allocated array if you want to be fancy. For this lesson, let's stick to a fixed array of 100 books. It keeps the memory management predictable while we focus on the logic.

Managing the library state

The heart of the system is a set of functions that mutate this array. You don't want your main function cluttered with logic; instead, create a "controller" style interface. I like to separate the "Adding" logic from the "Searching" logic to keep the code maintainable.

  • Add Book: Takes the current count, finds the next empty slot, and populates the struct.
  • Check Out: Searches for the book by ID and flips the isCheckedOut flag, but only if it's currently 0.
  • Return Book: The inverse of the check-out process.

One pro tip: when taking input for book titles, scanf("%s", ...) stops at the first space. Since most book titles have spaces, you should use fgets(). Just remember that fgets keeps the newline character \n at the end of the string, which will break your strcmp calls unless you strip it out first.




📋 Practical Task

Implementing a Book Borrowing and Return Toggle

Build a small Library Management program that implements the following functionality:

  • Define a Book struct with an id, title, and a status (0 for available, 1 for borrowed).
  • Create an array of 5 books and pre-populate them with some sample data.
  • Write a function void toggleBookStatus(Book library[], int id). This function should:
    • Search for the book with the matching id.
    • If found, flip its status (if it was 0, make it 1; if it was 1, make it 0).
    • Print a message stating whether the book was "Checked Out" or "Returned".
    • If the ID doesn't exist, print an error message.
  • In your main function, create a simple loop that allows the user to enter an ID to toggle its status until they enter a sentinel value (like -1) to exit.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.