luc114.c¶
Problem Statement
Store insurance policy holder info (gender, minor/major, policy name, duration) using bit-fields.
Metadata¶
| Property | Detail |
|---|---|
| Author | Amit Dutta amitdutta4255@gmail.com |
| Date | 08 Feb 2026 |
| License | MIT License (See the LICENSE file for details) |
Actions¶
💡 You can print or save this file by opening Raw and using your browser.
Source Code¶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define structure with bit-fields
struct policy_holder
{
char policy_name[50];
unsigned int duration : 7; // 0-127 years is sufficient for policy duration
unsigned int gender : 1; // 0: Male, 1: Female (1 bit)
unsigned int status : 1; // 0: Minor, 1: Major (1 bit)
};
int main()
{
struct policy_holder p;
int temp_gen, temp_stat, temp_dur;
printf("--- Enter Policy Holder Details ---\n");
printf("Policy Name: ");
scanf(" %[^\n]s", p.policy_name); // Reads string with spaces
printf("Duration (Years): ");
scanf("%d", &temp_dur);
p.duration = temp_dur;
printf("Gender (0 for Male, 1 for Female): ");
scanf("%d", &temp_gen);
p.gender = temp_gen;
printf("Status (0 for Minor, 1 for Major): ");
scanf("%d", &temp_stat);
p.status = temp_stat;
printf("\n--- Policy Information Stored ---\n");
printf("Policy: %s\n", p.policy_name);
printf("Duration: %u years\n", p.duration);
// Interpret bits for display
printf("Gender: %s\n", (p.gender == 1) ? "Female" : "Male");
printf("Status: %s\n", (p.status == 1) ? "Major" : "Minor");
printf("\nSize of structure: %zu bytes\n", sizeof(p));
// Note: Size will be policy_name size + padding + integer size containing the bits
return 0;
}