Skip to content

luc091.c

Problem Statement

Read 'blood donors' file (Name, Address, Age, Blood Type). Print donors with Age < 25 and Blood Type 2.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Intermediate (index: 5 / 10)

Concepts

Beta Feature

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

  • Pointers
  • Recursion
  • Array

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>
#include <ctype.h>

struct donor {
    char name[21];    // 20 cols + null
    char address[41]; // 40 cols + null
    int age;          // 2 cols -> int
    int blood_type;   // 1 col -> int
};

void create_donor_file();

int main()
{
    FILE *fp;
    struct donor d;

    create_donor_file();

    fp = fopen("donors.dat", "rb");
    if (!fp)
    {
        printf("File error.\n");
        exit(1);
    }

    printf("--- Donors (Age < 25, Type 2) ---\n");
    while (fread(&d, sizeof(struct donor), 1, fp) == 1)
    {
        if (d.age < 25 && d.blood_type == 2)
        {
            printf("Name: %s | Age: %d | Addr: %s\n", d.name, d.age, d.address);
        }
    }

    fclose(fp);
    return 0;
}

void create_donor_file()
{
    struct donor data[] = {
        {"Amit", "Delhi", 22, 2},   // Match
        {"Rahul", "Mumbai", 30, 2}, // Old
        {"Sumit", "Pune", 21, 1},   // Wrong type
        {"Priya", "Goa", 24, 2}     // Match
    };
    FILE *f = fopen("donors.dat", "wb");
    fwrite(data, sizeof(struct donor), 4, f);
    fclose(f);
}

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 (luc091.c):

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    struct donor {
        char name[21];    // 20 cols + null
        char address[41]; // 40 cols + null
        int age;          // 2 cols -> int
        int blood_type;   // 1 col -> int
    };

    void create_donor_file();

    int main()
    {
        FILE *fp;
        struct donor d;

        create_donor_file();

        fp = fopen("donors.dat", "rb");
        if (!fp)
        {
            printf("File error.\n");
            exit(1);
        }

        printf("--- Donors (Age < 25, Type 2) ---\n");
        while (fread(&d, sizeof(struct donor), 1, fp) == 1)
        {
            if (d.age < 25 && d.blood_type == 2)
            {
                printf("Name: %s | Age: %d | Addr: %s\n", d.name, d.age, d.address);
            }
        }

        fclose(fp);
        return 0;
    }

    void create_donor_file()
    {
        struct donor data[] = {
            {"Amit", "Delhi", 22, 2},   // Match
            {"Rahul", "Mumbai", 30, 2}, // Old
            {"Sumit", "Pune", 21, 1},   // Wrong type
            {"Priya", "Goa", 24, 2}     // Match
        };
        FILE *f = fopen("donors.dat", "wb");
        fwrite(data, sizeof(struct donor), 4, f);
        fclose(f);
    }