Skip to content

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)
Difficulty Beginner (index: 2 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Sorting (possible)
  • Iteration

Actions

Raw View on GitHub

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;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc114.c):

    #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;
    }