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