Library Management System In C

The idea of saving or documenting information about books integrated into the user's device is referred to as Library Management System. It includes information about the category of books, the inventory of books, and more. Access to the Library Management System is restricted to individuals with valid login credentials. Authorized users have the ability to carry out various tasks such as entering book information, deleting book records, viewing book details, updating book data, and more.

We need to employ the Library Management System to ensure the secure storage of book information housed in the Library (most likely). This functionality is typically activated to safeguard the highly sensitive data. The Library Management System is one of the most basic systems developed within the framework using the C programming language.

The Library Management System provides an overview of the process of storing and creating records for all the books housed in the library. It functions as a comprehensive database that houses detailed information about each book. By enabling efficient search capabilities, it minimizes the time required for locating specific details. Apart from safeguarding the book information, it ensures that all data remains current and intact, thereby eliminating any risk of information loss. This feature stands out as a key advantage of the Library Management System.

Let's create a C program that carries out tasks like "Inputting the information of a book", "Presenting the book information", and "Determining the overall quantity of books within the library".

A code example showcasing a Library Management System project implemented in the C programming language:

Example

// block 1
// to import standard input and output
#include <stdio.h>
// to import clear statements, etc. ( if any )
#include <conio.h>
// to import standard libraries
#include <stdlib.h>
// to import strings and use strings
#include <string.h>


// block 2

// declare all variables which store their respective 
// data using structure ' library '
struct library
{
    // to store the name of the book
    char book_name[100];
    // to store the name of the author of the book
    char author_name[100];
    // to store the cost of the book
    float cost;
    // to store the number of pages of the book
    int no_of_pages;
};


// block 3

// main function
int main()
{
    // using the strut library again.'
    // in order to perform operations
    struct library lib[100];
    char book_name[100];
    int i, j, count;
    i = 0;
    j = 0;
    count = 0;


    // block 3.1

    while(j!=6)
    {
        printf(" \n\n1. Add Book details\n ");
        printf(" 2. Display the list of books and its details\n ");
        printf(" 3. Display the total no. of books in the library\n ");
        printf(" 4. Exit\n\n");
        printf(" Enter the number: ");
        scanf(" %d", &j);


	   // block 3.1.1

        switch(j)
        {
            // in order to add the book details
            case 1:
                printf(" \nYou can add the details of the book ");
                printf(" \nEnter the book name: ");
                scanf(" %s", lib[i].book_name);
                printf(" \nEnter the author name: ");
                scanf(" %s", lib[i].author_name);
                printf(" \nEnter the number of pages: ");
                scanf(" %d", &lib[i].no_of_pages);
                printf(" \nEnter the cost of the book: ");
                scanf(" %f", &lib[i].cost);
                count = count + 1;
                i = i + 1;
                break;

            case 2:
                // if there are no books registered previously
                if (count==0)
                {
                    printf(" \nThere are no books stored!!\n\n ");
                }
                else
                {
                    // to view the list of the books
                    printf(" \nYou can view the list of books ");
                    printf(" \nThe list of books are: ");
                    for(i=0; i < count; i++)
                    {
                        printf(" \nThe name of the book is: %s ", lib[i].book_name);
                        printf(" \nThe name of the author is: %s ", lib[i].author_name);
                        printf(" \nThe number of pages are: %d ", lib[i].no_of_pages);
                        printf(" \nThe cost of the book is: %f\n\n ", lib[i].cost);
                    }
                }
                break;

            case 3:
                // to view the total number of books
                printf(" \nTotal number of books in the library are: %d\n\n ", count);
                break;
            
            case 4:
                // to exit from the program
                exit(0);
                
            default:
                // if any number other than 1, 2, 3, 4 is entered 
                printf(" \nInvalid number entered\n\n ");
        }

    }

}

An explanation for the program:

As we have organized the entire program into 5 segments, let's delve into each segment individually.

Block 1:

Example

// to import standard input and output
#include <stdio.h>
// to import clear statements, etc. ( if any )
#include <conio.h>
// to import standard libraries
#include <stdlib.h>
// to import strings and use strings
#include <string.h>

In this section, we've incorporated all the preprocessors essential for the program, instructing the C compiler to execute particular tasks prior to compilation. This particular phase, known as "Pre-processors," plays a crucial role in the "Structure of a C program." It's imperative to include specific preprocessors to accomplish necessary functions within the program.

Block 2:

Example

// declare all variables which store their respective 
// data using structure ' library '
struct library
{
    // to store the name of the book
    char book_name[100];
    // to store the name of the author of the book
    char author_name[100];
    // to store the cost of the book
    float cost;
    // to store the number of pages of the book
    int no_of_pages;
};

This section pertains to the " library " structure, which includes the obligatory variables. Typically, Structures in the C programming language serve the purpose of consolidating multiple variables into a single entity. These variables are defined and initialized within this block to be accessed whenever required in the program. Similarly, the " library " structure is employed here to define variables such as " bookname ", " authorname ", " cost ", and " noofpages ", each with distinct data types.

Block 3:

Example

// main function
int main()
{
    // using the strut library again.'
    // in order to perform operations
    struct library lib[100];
    char book_name[100];
    int i, j, count;
    i = 0;
    j = 0;
    count = 0;


    // block 3.1

    while(j!=6)
    {
        // statements regarding the choice

	   
   // block 3.1.1

        switch(j)
        {
            // switch cases depending on the option chosen
        }

    }

In this block of code, the primary section is initialized by directly using " int main ". Within the main section, we revisit the " library " structure that was previously defined to access its elements. To differentiate between the variable " book_name " declared in the structure and the one declared in the main section, the book's name can be redefined with a specific attribute.

The variables such as " bookname ", " i ", " j ", and " count " are initialized with specific purposes. " bookname " stores the name of the entered book, " i " and " j " are used for iteration, and " count " represents the total number of books in the library. Once these variables are assigned their respective data types, a while loop and a switch case are implemented. The following sections will delve into the functionalities of both the while loop and switch cases.

Block 3.1:

Example

while(j!=6)
{
        printf(" \n\n1. Add Book details\n ");
        printf(" 2. Display the list of books and its details\n ");
        printf(" 3. Display the total no. of books in the library\n ");
        printf(" 4. Exit\n\n");
        printf(" Enter the number: ");
        scanf(" %d", &j);

	   // block 3.1.1
        switch(j)
        {
            // switch cases depending on the option chosen
        }

}

In this code segment, the process of iteration is carried out utilizing a while loop. It is essential to start the iteration to handle various inputs and generate their respective outputs. Essentially, the while loop is employed to iterate multiple times when there is a need to provide input repetitively, enabling the retrieval of comprehensive information about the books available in the library.

A variable "j" initialized to 0 is taken into account, along with a fundamental condition "j != 6". Within the while loop, this condition guarantees that the sequence number or the selection between the options "Add Book details", "Display the list of books and their details", "Display the total number of books in the library", and "Exit" does not surpass the limit. If the number 5 is inputted, it functions as the default and terminates the loop, triggering the display of the "Invalid Input" message.

There is a " switch case " statement added, which evaluates the stored value of j from before and then selects the case based on the scanned number. Further details regarding the switch case block will be covered in the following paragraphs.

Block 3.1.1:

Example

switch(j)
        {
            // in order to add the book details
            case 1:
                printf(" \nYou can add the details of the book ");
                printf(" \nEnter the book name: ");
                scanf(" %s", lib[i].book_name);
                printf(" \nEnter the author name: ");
                scanf(" %s", lib[i].author_name);
                printf(" \nEnter the number of pages: ");
                scanf(" %d", &lib[i].no_of_pages);
                printf(" \nEnter the cost of the book: ");
                scanf(" %f", &lib[i].cost);
                count = count + 1;
                i = i + 1;
                break;

            case 2:
                // if there are no books registered previously
                if (count==0)
                {
                    printf(" \nThere are no books stored!!\n\n ");
                }
                else
                {
                    // to view the list of the books
                    printf(" \nYou can view the list of books ");
                    printf(" \nThe list of books are: ");
                    for(i=0; i < count; i++)
                    {
                        printf(" \nThe name of the book is: %s ", lib[i].book_name);
                        printf(" \nThe name of the author is: %s ", lib[i].author_name);
                        printf(" \nThe number of pages are: %d ", lib[i].no_of_pages);
                        printf(" \nThe cost of the book is: %f\n\n ", lib[i].cost);
                    }
                }
                break;

            case 3:
                // to view the total number of books
                printf(" \nTotal number of books in the library are: %d\n\n ", count);
                break;
            
            case 4:
                // to exit from the program
                exit(0);
                
            default:
                // if any number other than 1, 2, 3, 4 is entered 
                printf(" \nInvalid number entered\n\n ");
        }

In this code block, each possible case is accounted for, and the corresponding output is provided accordingly. For instance, if the input number is 2 from the selection of 1, 2, 3, 4, the output will display the list of books from the library as the second case represents the "display the list of books in the library" action. Similarly, when the input number is 1, it captures details about a new book as the first case corresponds to "Add book details". In the event that an input number outside the range of 1, 2, 3, or 4 is entered, the default case is triggered, and it subsequently prints "Invalid number entered" since there are no defined actions for numbers other than 1, 2, 3, and 4.

Input and output for the above program:

Example

1. Add Book details
2. Display the list of books and its details
3. Display the total no. of books in the library
4. Exit

Enter the number: 1

You can add the details of the book  
Enter the book name: Nikki's_life
 
Enter the author name: Nikhitha
 
Enter the number of pages: 306
 
Enter the cost of the book: 500.00
Example

1. Add Book details
2. Display the list of books and its details
3. Display the total no. of books in the library
4. Exit

Enter the number: 1
 
You can add the details of the book
Enter the book name: Nikki_schooling
 
Enter the author name: Nikhitha
 
Enter the number of pages: 490
 
Enter the cost of the book: 1000
Example

1. Add Book details
2. Display the list of books and its details
3. Display the total no. of books in the library
4. Exit

Enter the number: 1
 
You can add the details of the book
Enter the book name: Sathwik_journey
 
Enter the author name: Sathwik
 
Enter the number of pages: 1048
 
Enter the cost of the book: 2500
Example

1. Add Book details
2. Display the list of books and its details
3. Display the total no. of books in the library
4. Exit

Enter the number: 2
 
You can view the list of books
The list of books are:
The name of the book is: Nikki's_life
The name of the author is: Nikhitha
The number of pages are: 306
The cost of the book is: 500.000000

The name of the book is: Nikki_schooling
The name of the author is: Nikhitha  
The number of pages are: 490
The cost of the book is: 1000.000000

The name of the book is: Sathwik_journey
The name of the author is: Sathwik
The number of pages are: 1048
The cost of the book is: 2500.000000

In this manner, the Library Management System program is crafted using fundamental blocks of code. This method stands out as a top-notch approach for storing data, particularly data associated with books within the library.

Input Required

This code uses input(). Please provide values below: