luc114.c (1844B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Store insurance policy holder info (gender, minor/major, policy name, duration) using bit-fields. 9 */ 10 /* Let Us C, Chap- 22 (Miscellaneous Features), Qn No.: C(b) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 20 // Define structure with bit-fields 21 struct policy_holder 22 { 23 char policy_name[50]; 24 unsigned int duration : 7; // 0-127 years is sufficient for policy duration 25 unsigned int gender : 1; // 0: Male, 1: Female (1 bit) 26 unsigned int status : 1; // 0: Minor, 1: Major (1 bit) 27 }; 28 29 int main() 30 { 31 struct policy_holder p; 32 int temp_gen, temp_stat, temp_dur; 33 34 printf("--- Enter Policy Holder Details ---\n"); 35 36 printf("Policy Name: "); 37 scanf(" %[^\n]s", p.policy_name); // Reads string with spaces 38 39 printf("Duration (Years): "); 40 scanf("%d", &temp_dur); 41 p.duration = temp_dur; 42 43 printf("Gender (0 for Male, 1 for Female): "); 44 scanf("%d", &temp_gen); 45 p.gender = temp_gen; 46 47 printf("Status (0 for Minor, 1 for Major): "); 48 scanf("%d", &temp_stat); 49 p.status = temp_stat; 50 51 printf("\n--- Policy Information Stored ---\n"); 52 printf("Policy: %s\n", p.policy_name); 53 printf("Duration: %u years\n", p.duration); 54 55 // Interpret bits for display 56 printf("Gender: %s\n", (p.gender == 1) ? "Female" : "Male"); 57 printf("Status: %s\n", (p.status == 1) ? "Major" : "Minor"); 58 59 printf("\nSize of structure: %zu bytes\n", sizeof(p)); 60 // Note: Size will be policy_name size + padding + integer size containing the bits 61 62 return 0; 63 }