Monday, August 17, 2026

Library Book Details || C Lab Programs

Program 14: Store and display library book details using structures.

Problem Statement:: A local library needs to store and display details of its books, including the title, author, and year of publication. Design a structure that can hold these details and develop a C program to display a list of all books entered.

Problem Description:

  • Input: The total number of books (n), followed by the title (string), author (string), and year of publication (integer) for each book.

  • Output: A formatted list displaying each book's details in the format: "Book i: Title by Author (Year)".

  • Constraints: The number of books must be greater than zero; the year must be a positive integer; input strings should be less than 50 characters.

  • Method: Define a struct Book to organize the disparate data types (strings and integers) into a single record.

Pgm Logic:

  1. Start.

  2. Define a structure named Book containing character arrays for title and author, and an integer for year.

  3. Input the total number of books n.

  4. Declare an array of structures named booklist of size n.

  5. Use a for-loop to iterate from 0 to n-1:

    • Prompt and read the title, author, and year for each book using a method that accepts spaces.

  6. Use another for-loop to iterate through the array:

    • Display the stored details for each book in the required format.

  7. Stop.

Program Code:

// Purpose: To store and display library book details using C structures.

#include <stdio.h>


// Define the structure to hold diverse book details

struct Book {

    char title[15];

    char author[15];

    int year;

};


int main() {

    int n, i;

    printf("Enter number of books: ");

    scanf("%d", &n);

    

    // Declare an array of structures based on user input size

    struct Book booklist[n];

    

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

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

        

        printf("Title: ");

        // " %[^\n]s" reads the entire line including spaces

        scanf(" %[^\n]s", booklist[i].title);

        

        printf("Author: ");

        scanf(" %[^\n]s", booklist[i].author);

        

        printf("Year of Publication: ");

        scanf("%d", &booklist[i].year);

    }

    

    printf("\nLibrary Book Details:\n");

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

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

        // Accessing structure members using the dot (.) operator

        printf("Book %d: %s by %s (%d)\n", i + 1, booklist[i].title, booklist[i].author, booklist[i].year);

    }

    

    return 0;

}


Output: 

Enter number of books: 2

Enter details for book 1: 

Title: C Programming 

Author: Dennis Ritchie 

Year of Publication: 1978

Enter details for book 2: 

Title: Digital Design 

Author: Morris Mano 

Year of Publication: 2017


Library Book Details:

Book 1: C Programming by Dennis Ritchie (1978) 

Book 2: Digital Design by Morris Mano (2017)


RESULT: Thus the program has been executed and the output was verified.

Remarks: This program was compiled and run in the Code::Blocks IDE. It demonstrates how structures can act like a mini-database in memory. Using the space before %[ in scanf is essential to clear any leftover newline characters from the previous numeric input.

Program Explanation: The program uses a struct to group a book's title, author, and year into a single unit. By creating an array of these structures, we can store multiple book records efficiently. The code utilizes a loop to collect data for each book and another loop to traverse the array and print the details using the dot (.) operator to access individual fields within each structure.


For all 2026 published C Lab Program posts Index page: click here

For all 2026 published articles list: click here

…till the next post, bye-bye & take care