Self-Study Structure Union Bitfields Vasantha Balan K
Self-Study Structure Union Bitfields Vasantha Balan K
Q.
Question
No
1 Write a program that compares two given dates. To store date use
structure, say date that contains three members namely date, month and
year. If the dates are equal then display message as "Equal" otherwise
"Unequal".
Sample Output
Equal
Sample Input
Enter first date:
Enter day: 25
Enter month: 12
Enter year: 2021
Sample Output
Unequal
Solution
#include <stdio.h>
struct Date {
unsigned int day : 5; // 5 bits to store day (0-31)
unsigned int month : 4; // 4 bits to store month (0-12)
unsigned int year : 12; // 12 bits to store year (0-4095)
};
int main() {
struct Date date1, date2;
if (compareDates(date1, date2)) {
printf("Equal\n");
} else {
printf("Unequal\n");
}
return 0;
}
2 Write a menu driven program that depicts the working of a library. The
menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the
book, author name, price of the book, and flag indicating whether book is
issued or not.
Book 1:
Accession Number: 101
Title: C Programming Language
Author: Dennis Ritchie
Price: $29.99
Issued: No
Book 2:
Accession Number: 102
Title: Algorithms Unlocked
Author: Thomas Cormen
Price: $39.99
Issued: Yes
Book 3:
Accession Number: 103
Title: Clean Code
Author: Robert Martin
Price: $45.50
Issued: No
Solution
#include <stdio.h>
#include <string.h>
struct Library {
int accessionNumber;
char title[100];
char author[100];
float price;
int isIssued; // 1 if issued, 0 if not issued
};
int bookCount = 0;
struct Library books[MAX_BOOKS];
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("Library is full. Cannot add more books.\n");
return;
}
books[bookCount++] = newBook;
printf("Book added successfully.\n");
}
void displayBooks() {
if (bookCount == 0) {
printf("No books in the library.\n");
return;
void listBooksByAuthor() {
char author[100];
getchar(); // clear newline character
printf("Enter author name: ");
fgets(author, 100, stdin);
author[strcspn(author, "\n")] = 0; // remove newline
int found = 0;
printf("\nBooks by author '%s':\n", author);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].author, author) == 0) {
printf("Title: %s\n", books[i].title);
found = 1;
}
}
if (!found) {
printf("No books found by author '%s'.\n", author);
}
}
void listBookTitle() {
int accessionNumber;
printf("Enter accession number: ");
scanf("%d", &accessionNumber);
void countBooks() {
printf("Total number of books in the library: %d\n", bookCount);
}
void listBooksByAccessionNumber() {
struct Library temp;
int main() {
int choice;
do {
printf("\nLibrary Management System\n");
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
listBooksByAuthor();
break;
case 4:
listBookTitle();
break;
case 5:
countBooks();
break;
case 6:
listBooksByAccessionNumber();
break;
case 7:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 7);
return 0;
2.
3.
4.
5.