C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures and Union in C

Library management using structures

C Program: Library management using structures

C

#include <stdio.h>

#include <string.h>

 

// Structure to store book details

struct Book {

    int id;

    char title[100];

    char author[100];

    float price;

};

 

int main() {

    struct Book library[100];

    int n, i, choice, searchID, found;

 

    printf("Enter number of books: ");

    scanf("%d", &n);

 

    // Input details of books

    for (i = 0; i < n; i++) {

        printf("\nEnter details of Book %d:\n", i + 1);

        printf("Book ID: ");

        scanf("%d", &library[i].id);

        getchar(); // clear newline character

 

        printf("Title: ");

        fgets(library[i].title, sizeof(library[i].title), stdin);

        library[i].title[strcspn(library[i].title, "\n")] = '\0'; // remove newline

 

        printf("Author: ");

        fgets(library[i].author, sizeof(library[i].author), stdin);

        library[i].author[strcspn(library[i].author, "\n")] = '\0';

 

        printf("Price: ");

        scanf("%f", &library[i].price);

    }

 

    do {

        printf("\n========= Library Management Menu =========\n");

        printf("1. Display all books\n");

        printf("2. Search book by ID\n");

        printf("3. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                printf("\n------------------- Library Books -------------------\n");

                printf("%-10s %-30s %-20s %-10s\n", "Book ID", "Title", "Author", "Price");

                printf("-----------------------------------------------------\n");

                for (i = 0; i < n; i++) {

                    printf("%-10d %-30s %-20s %-10.2f\n",

                           library[i].id, library[i].title,

                           library[i].author, library[i].price);

                }

                break;

 

            case 2:

                printf("\nEnter Book ID to search: ");

                scanf("%d", &searchID);

                found = 0;

                for (i = 0; i < n; i++) {

                    if (library[i].id == searchID) {

                        printf("\nBook Found:\n");

                        printf("ID: %d\nTitle: %s\nAuthor: %s\nPrice: %.2f\n",

                               library[i].id, library[i].title,

                               library[i].author, library[i].price);

                        found = 1;

                        break;

                    }

                }

                if (!found)

                    printf("Book with ID %d not found.\n", searchID);

                break;

 

            case 3:

                printf("\nExiting Library Management System...\n");

                break;

 

            default:

                printf("Invalid choice! Try again.\n");

        }

 

    } while (choice != 3);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of books: 2

Enter details of Book 1:
Book ID: 101
Title: C Programming
Author: Dennis Ritchie
Price: 250

Enter details of Book 2:
Book ID: 102
Title: Data Structures
Author: Mark Allen Weiss
Price: 300

========= Library Management Menu =========
1. Display all books
2. Search book by ID
3. Exit
Enter your choice: 1

------------------- Library Books ------------------------------
Book ID    Title                          Author              Price     
------------------------------------------------------------------
101        C Programming        Dennis Ritchie       250.00    
102        Data Structures       Mark Allen Weiss    300.00    
------------------------------------------------------------------

Enter your choice: 2
Enter Book ID to search: 101

Book Found:
ID: 101
Title: C Programming
Author: Dennis Ritchie
Price: 250.00

Enter your choice: 3
Exiting Library Management System...

Explanation

Concept

Description

struct Book

Holds book ID, title, author, and price.

library[100]

Array of book records (up to 100).

fgets()

Reads strings safely, including spaces.

strcspn()

Removes the newline character from input.

Menu

Provides simple options to view or search books.