C Lesson 50 – C Capstone Project | Dataplexa

C Capstone Project – Employee Management System

This is the final lesson of the C Programming course.

In this lesson, we build a complete working project using C, exactly the way it is done in real applications.


Project Overview

We will build a Console-Based Employee Management System.

This project uses:

  • Structures
  • Functions
  • File handling
  • Menu-driven logic
  • Real-world data storage

What This Project Can Do

  • Add employee records
  • View all employees
  • Search employee by ID
  • Store data permanently in a file

Complete Program Code


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

struct Employee {
    int id;
    char name[50];
    char department[30];
    float salary;
};

void addEmployee();
void viewEmployees();
void searchEmployee();

int main() {
    int choice;

    while (1) {
        printf("\n===== Employee Management System =====\n");
        printf("1. Add Employee\n");
        printf("2. View All Employees\n");
        printf("3. Search Employee by ID\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addEmployee();
                break;
            case 2:
                viewEmployees();
                break;
            case 3:
                searchEmployee();
                break;
            case 4:
                printf("Exiting program...\n");
                exit(0);
            default:
                printf("Invalid choice. Try again.\n");
        }
    }
}

Explanation of the Code

struct Employee groups employee data such as ID, name, department and salary.

The program uses a menu-driven infinite loop so the user can perform multiple operations without restarting the program.


Add Employee Function


void addEmployee() {
    struct Employee emp;
    FILE *fp = fopen("employees.dat", "ab");

    if (fp == NULL) {
        printf("File error\n");
        return;
    }

    printf("Enter Employee ID: ");
    scanf("%d", &emp.id);

    printf("Enter Name: ");
    scanf(" %[^\n]", emp.name);

    printf("Enter Department: ");
    scanf(" %[^\n]", emp.department);

    printf("Enter Salary: ");
    scanf("%f", &emp.salary);

    fwrite(&emp, sizeof(emp), 1, fp);
    fclose(fp);

    printf("Employee added successfully.\n");
}

This function writes employee data into a binary file using fwrite().


View Employees Function


void viewEmployees() {
    struct Employee emp;
    FILE *fp = fopen("employees.dat", "rb");

    if (fp == NULL) {
        printf("No records found.\n");
        return;
    }

    while (fread(&emp, sizeof(emp), 1, fp)) {
        printf("\nID: %d", emp.id);
        printf("\nName: %s", emp.name);
        printf("\nDepartment: %s", emp.department);
        printf("\nSalary: %.2f\n", emp.salary);
    }

    fclose(fp);
}

This function reads all employee records from the file and displays them.


Search Employee Function


void searchEmployee() {
    struct Employee emp;
    FILE *fp = fopen("employees.dat", "rb");
    int id, found = 0;

    if (fp == NULL) {
        printf("File not found.\n");
        return;
    }

    printf("Enter Employee ID to search: ");
    scanf("%d", &id);

    while (fread(&emp, sizeof(emp), 1, fp)) {
        if (emp.id == id) {
            printf("\nEmployee Found!");
            printf("\nName: %s", emp.name);
            printf("\nDepartment: %s", emp.department);
            printf("\nSalary: %.2f\n", emp.salary);
            found = 1;
            break;
        }
    }

    if (!found)
        printf("Employee not found.\n");

    fclose(fp);
}

Sample Output


===== Employee Management System =====
1. Add Employee
2. View All Employees
3. Search Employee by ID
4. Exit
Enter your choice: 1

Enter Employee ID: 101
Enter Name: John
Enter Department: IT
Enter Salary: 55000
Employee added successfully.


Mini Practice

  • Add update and delete employee options
  • Sort employees by salary
  • Export data to text file

Final Note

If you understand and can modify this project, you are ready for real-world C programming.

Well done on completing the C Programming course.