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?
book with variables name, pages, and rate.b to access structure members.#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(); }
structure to store and display student details such as student name, roll number, and marks.student with members name, rollno, and marks. Create a structure variable and accept values from the user. Finally, display all student details.#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(); }
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.