Book Structure Example


Explanation

A structure in C language is a user-defined data type that allows us to store different types of data under one name. Unlike arrays, structures can store data of different data types such as integer, character, and float together.

In this program, a structure named book is created to store book details like book name, number of pages, and book rate. A structure variable is used to accept and display the details of a book.

How It Works?

  • Step 1: Define a structure named book with variables name, pages, and rate.
  • Step 2: Create a structure variable b to access structure members.
  • Step 3: Accept book name, pages, and rate from the user.
  • Step 4: Store the entered values inside structure members.
  • Step 5: Display all book details using the structure variable.

Example Program


#include <stdio.h>
#include <conio.h>

struct book
{

    char name[20];

    int rate, pages;

};


main()
{

    struct book b;

    clrscr();


    printf("Enter the Book Name : ");

    scanf("%s", b.name);


    printf("Enter the Book pages : ");

    scanf("%d", &b.pages);


    printf("Enter the Book rate : ");

    scanf("%d", &b.rate);


    printf("\nBook Name is %s", b.name);

    printf("\nBook Pages are %d", b.pages);

    printf("\nBook Rate is %d", b.rate);


    getch();

}

Output

Enter the Book Name : CProgramming

Enter the Book pages : 250

Enter the Book rate : 500

Book Name is CProgramming
Book Pages are 250
Book Rate is 500

Practice Exercises

Task 1: Write a C program using structure to store and display student details such as student name, roll number, and marks.

Goal: Learn how to create and use a structure to store different types of data together.

💡 Hint: Create a structure named student with members name, rollno, and marks. Create a structure variable and accept values from the user. Finally, display all student details.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];

    int rollno, marks;

};


main()
{

    struct student s;

    clrscr();


    printf("Enter Student Name : ");

    scanf("%s", s.name);


    printf("\nEnter Roll Number : ");

    scanf("%d", &s.rollno);


    printf("\nEnter Marks : ");

    scanf("%d", &s.marks);


    printf("\nStudent Name is %s", s.name);

    printf("\nRoll Number is %d", s.rollno);

    printf("\nMarks are %d", s.marks);


    getch();

}

Output
Enter Student Name : Rahul

Enter Roll Number : 101

Enter Marks : 85

Student Name is Rahul
Roll Number is 101
Marks are 85
Explanation: The program creates a structure named student to store student information. The structure variable s is used to store name, roll number, and marks. After accepting the values from the user, all student details are displayed.