Pages

Wednesday, April 12, 2017

Array of Structure

This program will demonstrate Array of Structure.

#include <stdio.h>
#include <string.h>

struct Book{
   int   book_id;
   char  book_title[30];
   char  book_publisher[30];
   char  book_subject[30];
};

int main( ) {

// Declare 3 variables of Book structure

 struct Book myBook[3];  
int i;

//Initialise values to myBook variable
for(i=0; i<3; i++)
{
printf("\nEnter Details of Book%d:\n",i+1);
printf("ID:");
scanf("%d",&myBook[i].book_id);

printf("Title:");
  scanf("%s",myBook[i].book_title);
  
  printf("Publisher Name:");
    scanf("%s",myBook[i].book_publisher);
  
    printf("Subject:");
    scanf("%s", myBook[i].book_subject);
}
   
   /* print myBook details */
   for(i=0; i<3; i++)
   {
    printf("\nDetails of Book%d:\n",i+1);
    printf( "ID: %d\n", myBook[i].book_id);
    printf( "Title: %s\n", myBook[i].book_title);
    printf( "Publisher Name : %s\n", myBook[i].book_publisher);
    printf( "Subject : %s\n", myBook[i].book_subject);
    printf("\n");
   }
   return 0;
}

Output of program


Enter Details of Book1:
ID:101
Title:Math-1
Publisher Name:ABC
Subject:Mathematics

Enter Details of Book2:
ID:102
Title:DataScience
Publisher Name:XYZ
Subject:Science

Enter Details of Book3:
ID:103
Title:Security
Publisher Name:ABC
Subject:Cryptography

Details of Book1:
ID: 101
Title: Math-1
Publisher Name : ABC
Subject : Mathematics


Details of Book2:
ID: 102
Title: DataScience
Publisher Name : XYZ
Subject : Science


Details of Book3:
ID: 103
Title: Security
Publisher Name : ABC
Subject : Cryptography


No comments:

Post a Comment