Using structures, write the necessary declaration for information about books in a library.
Source Code
Brief explanation is provided after the source code.
#include <stdio.h> typedef struct { char *isbn[10]; char *title[255]; short int edition_number; char *copyright[4]; } BOOK; int main(int argc, char ** argv) { BOOK book; printf("ISBN: "); scanf("%s", book.isbn); printf("\nTitle: "); scanf("%s", book.title); printf("\nEdition Number: "); scanf("%d", &book.edition_number); printf("\nCopyright: "); scanf("%s", book.copyright); printf("\nISBN: "); printf("%s", book.isbn); printf("\nTitle: "); printf("%s", book.title); printf("\nEdition Number: "); printf("%d", book.edition_number); printf("\nCopyright: "); printf("%s\n", book.copyright); return 0; }
When you compile and execute the above program it produces the following result on Linux:
ISBN: 978-1-84078-544-9 Title: C-Programming-in-Easy-Steps Edition Number: 4 Copyright: 2012 ISBN: 978-1-84078-544-9 Title: C-Programming-in-Easy-Steps Edition Number: 4 Copyright: 2012
Brief Explanation
- A structure in C is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
- Using typedef, BOOK is created as a synonym for the structure. BOOK is then use in the main function to define a variable book of type structure. i.e BOOK book;
- scanf function reads data and assign to the fields define in the structure.
- Using printf function, the inputted data is displayed in a nice format to the stdout.