The structure is a collection of dissimilar data types.
![]() |
| Structure in C++ | it2Edu |
Declaration of structure doesn’t reserve memory, it is only reserved when variables of structure data types are created.
Syntax :
struct structure_name
{
//variable declaration
};
Eaxmple :
#include<iostream>
using namespace std;
//structure
struct bookDetails
{
char name;
int pages;
float price;
};
//main declaration
int main()
{
struct bookDetails b;
cout<<"Enter name of book : \n";
cin>>b.name;
cout<<"\nEnter number of pages in book : \n";
cin>>b.pages;
cout<<"\nEnter price of book : \n";
cin>>b.price;
cout<<"\nDetails of book is : \n";
cout<<"\nbook name : "<<b.name;
cout<<"\nnumber of pages : "<<b.pages;
cout<<"\nprice of book : "<<b.price;
}
Output :
Enter No Of Books : 1
Enter the book details
Details of Book Number : 1
Book Name separate it by underscore : harry_potter
Book Author :j.k.rowling
| S.No | Book Name | Author |
|---|---|---|
| 1 | harry_potter | j.k.rowling |
NOTE: Structure declaration must end with a semicolon.
