Recommended Reading
Notes Version | A very concise summary of C language knowledge!
Complete Source Code | C Language Design Cross-Platform Logging Library | Enterprise-Level Development
GitHub Stars 88.9K, 9 amazing open-source projects!
Latest C Language Interview Questions Summary PDF Detailed Version
Understand “Stack Overflow” and “Heap Overflow” in C Language, the most straightforward explanation on the internet
Main Content
System Overview
This is a simple employee attendance management system designed and implemented in C language, allowing for the recording and querying of employee attendance information, including check-in, check-out, leave requests, and attendance statistics.
System Design
1. Data Structures:
- The Employee structure stores basic employee information
- The AttendanceRecord structure stores attendance records
- The AttendanceSystem structure manages all system data
2. Main Functions:
- Add employee information
- Check-in/Check-out records
- Leave application
- Attendance record query
- Attendance report generation
- Data persistent storage
Function Flowchart
Start
|
Initialize System
|
Display Main Menu
|
Input Selection
|
+----------------+----------------+----------------+----------------+----------------+
| | | | | |
Check-in(1) Check-out(2) Leave(3) Query Records(4) Statistics(5) Exit(0)
| | | | | |
Record Check-in Time Record Check-out Time Record Leave Info Display Attendance Records Display Attendance Statistics Save Data
| | | | | |
+----------------+----------------+----------------+----------------+----------------+
|
Save Data
|
End
Function Description
- Add Employee: Input employee ID, name, and department information, the system will check if the ID is duplicated
- Check-in: Record the employee’s work start time, automatically get the current time
- Check-out: Record the employee’s work end time, automatically get the current time
- Leave: Employees can apply for personal leave, sick leave, or annual leave, and specify the reason
- Query Records: Query specific employee attendance records by time range
- Generate Report: Statistics of all employees’ attendance in the specified time period
User Instructions
- The program will automatically load previously saved data upon startup
- Select the operation to be performed through the menu
- Input the corresponding information as prompted
- All data will be automatically saved to a file when exiting the program
Complete Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Define Employee structure
typedef struct {
int id;
char name[50];
char department[50];
} Employee;
// Define Attendance Record structure
typedef struct {
int employeeId;
char date[11]; // YYYY-MM-DD
char checkIn[6]; // HH:MM
char checkOut[6]; // HH:MM
int leaveType; // 0: No Leave, 1: Personal Leave, 2: Sick Leave, 3: Annual Leave
char remarks[100];
} AttendanceRecord;
// Define system data structure
typedef struct {
Employee* employees;
AttendanceRecord* records;
int employeeCount;
int recordCount;
int maxEmployees;
int maxRecords;
} AttendanceSystem;
// Function declarations
void initializeSystem(AttendanceSystem* system, int maxEmployees, int maxRecords);
void freeSystem(AttendanceSystem* system);
void displayMenu();
void addEmployee(AttendanceSystem* system);
void checkIn(AttendanceSystem* system);
void checkOut(AttendanceSystem* system);
void applyLeave(AttendanceSystem* system);
void queryRecords(AttendanceSystem* system);
void generateReport(AttendanceSystem* system);
void saveData(AttendanceSystem* system);
void loadData(AttendanceSystem* system);
int findEmployeeById(AttendanceSystem* system, int id);
int findRecord(AttendanceSystem* system, int employeeId, const char* date);
void getCurrentDate(char* date);
void getCurrentTime(char* time);
int main() {
AttendanceSystem system;
int choice;
// Initialize system
initializeSystem(&system, 100, 1000);
// Load data
loadData(&system);
printf("Welcome to the Employee Attendance Management System!\n");
do {
displayMenu();
printf("Please select an operation: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addEmployee(&system);
break;
case 2:
checkIn(&system);
break;
case 3:
checkOut(&system);
break;
case 4:
applyLeave(&system);
break;
case 5:
queryRecords(&system);
break;
case 6:
generateReport(&system);
break;
case 0:
printf("Thank you for using the Employee Attendance Management System!\n");
break;
default:
printf("Invalid selection, please re-enter!\n");
}
} while(choice != 0);
// Save data
saveData(&system);
// Free system resources
freeSystem(&system);
return 0;
}
// Initialize system
void initializeSystem(AttendanceSystem* system, int maxEmployees, int maxRecords) {
system->employees = (Employee*)malloc(maxEmployees * sizeof(Employee));
system->records = (AttendanceRecord*)malloc(maxRecords * sizeof(AttendanceRecord));
system->employeeCount = 0;
system->recordCount = 0;
system->maxEmployees = maxEmployees;
system->maxRecords = maxRecords;
}
// Free system resources
void freeSystem(AttendanceSystem* system) {
free(system->employees);
free(system->records);
}
// Display menu
void displayMenu() {
printf("\n===== Employee Attendance Management System =====\n");
printf("1. Add Employee\n");
printf("2. Check-in\n");
printf("3. Check-out\n");
printf("4. Leave\n");
printf("5. Query Attendance Records\n");
printf("6. Generate Attendance Report\n");
printf("0. Exit System\n");
printf("============================\n");
}
// Add employee
void addEmployee(AttendanceSystem* system) {
if(system->employeeCount >= system->maxEmployees) {
printf("Employee limit reached, cannot add new employee!\n");
return;
}
Employee newEmployee;
printf("Please enter employee ID: ");
scanf("%d", &newEmployee.id);
// Check if employee ID already exists
if(findEmployeeById(system, newEmployee.id) != -1) {
printf("Employee ID already exists!\n");
return;
}
printf("Please enter employee name: ");
scanf("%s", newEmployee.name);
printf("Please enter employee department: ");
scanf("%s", newEmployee.department);
system->employees[system->employeeCount] = newEmployee;
system->employeeCount++;
printf("Employee added successfully!\n");
}
// Check-in
void checkIn(AttendanceSystem* system) {
int employeeId;
char date[11];
char time[6];
printf("Please enter employee ID: ");
scanf("%d", &employeeId);
int employeeIndex = findEmployeeById(system, employeeId);
if(employeeIndex == -1) {
printf("Employee does not exist!\n");
return;
}
getCurrentDate(date);
getCurrentTime(time);
int recordIndex = findRecord(system, employeeId, date);
if(recordIndex == -1) {
// Create new record
if(system->recordCount >= system->maxRecords) {
printf("Attendance record limit reached!\n");
return;
}
AttendanceRecord newRecord;
newRecord.employeeId = employeeId;
strcpy(newRecord.date, date);
strcpy(newRecord.checkIn, time);
strcpy(newRecord.checkOut, "00:00");
newRecord.leaveType = 0;
strcpy(newRecord.remarks, "Check-in");
system->records[system->recordCount] = newRecord;
system->recordCount++;
printf("Check-in successful! Time: %s %s\n", date, time);
} else {
// Update existing record
if(strcmp(system->records[recordIndex].checkIn, "00:00") != 0) {
printf("Already checked in today! Check-in time: %s\n", system->records[recordIndex].checkIn);
return;
}
strcpy(system->records[recordIndex].checkIn, time);
printf("Check-in successful! Time: %s %s\n", date, time);
}
}
// Check-out
void checkOut(AttendanceSystem* system) {
int employeeId;
char date[11];
char time[6];
printf("Please enter employee ID: ");
scanf("%d", &employeeId);
int employeeIndex = findEmployeeById(system, employeeId);
if(employeeIndex == -1) {
printf("Employee does not exist!\n");
return;
}
getCurrentDate(date);
getCurrentTime(time);
int recordIndex = findRecord(system, employeeId, date);
if(recordIndex == -1) {
printf("Please check in today first!\n");
return;
}
if(strcmp(system->records[recordIndex].checkOut, "00:00") != 0) {
printf("Already checked out today! Check-out time: %s\n", system->records[recordIndex].checkOut);
return;
}
strcpy(system->records[recordIndex].checkOut, time);
strcat(system->records[recordIndex].remarks, ", Check-out");
printf("Check-out successful! Time: %s %s\n", date, time);
}
// Leave
void applyLeave(AttendanceSystem* system) {
int employeeId;
char date[11];
int leaveType;
char remarks[100];
printf("Please enter employee ID: ");
scanf("%d", &employeeId);
int employeeIndex = findEmployeeById(system, employeeId);
if(employeeIndex == -1) {
printf("Employee does not exist!\n");
return;
}
printf("Please enter leave date (YYYY-MM-DD): ");
scanf("%s", date);
printf("Please select leave type (1. Personal Leave 2. Sick Leave 3. Annual Leave): ");
scanf("%d", &leaveType);
printf("Please enter leave reason: ");
scanf("%s", remarks);
int recordIndex = findRecord(system, employeeId, date);
if(recordIndex == -1) {
// Create new record
if(system->recordCount >= system->maxRecords) {
printf("Attendance record limit reached!\n");
return;
}
AttendanceRecord newRecord;
newRecord.employeeId = employeeId;
strcpy(newRecord.date, date);
strcpy(newRecord.checkIn, "00:00");
strcpy(newRecord.checkOut, "00:00");
newRecord.leaveType = leaveType;
strcpy(newRecord.remarks, remarks);
system->records[system->recordCount] = newRecord;
system->recordCount++;
} else {
// Update existing record
system->records[recordIndex].leaveType = leaveType;
strcpy(system->records[recordIndex].remarks, remarks);
}
printf("Leave application submitted successfully!\n");
}
// Query attendance records
void queryRecords(AttendanceSystem* system) {
int employeeId;
char startDate[11], endDate[11];
printf("Please enter employee ID: ");
scanf("%d", &employeeId);
int employeeIndex = findEmployeeById(system, employeeId);
if(employeeIndex == -1) {
printf("Employee does not exist!\n");
return;
}
printf("Please enter start date (YYYY-MM-DD): ");
scanf("%s", startDate);
printf("Please enter end date (YYYY-MM-DD): ");
scanf("%s", endDate);
printf("\nAttendance records for %s (%s to %s):\n",
system->employees[employeeIndex].name, startDate, endDate);
printf("Date\t\tCheck-in Time\tCheck-out Time\tLeave Type\tRemarks\n");
int found = 0;
for(int i = 0; i < system->recordCount; i++) {
if(system->records[i].employeeId == employeeId &&
strcmp(system->records[i].date, startDate) >= 0 &&
strcmp(system->records[i].date, endDate) <= 0) {
char* leaveTypeStr;
switch(system->records[i].leaveType) {
case 0: leaveTypeStr = "None"; break;
case 1: leaveTypeStr = "Personal Leave"; break;
case 2: leaveTypeStr = "Sick Leave"; break;
case 3: leaveTypeStr = "Annual Leave"; break;
default: leaveTypeStr = "Unknown";
}
printf("%s\t%s\t\t%s\t\t%s\t\t%s\n",
system->records[i].date,
system->records[i].checkIn,
system->records[i].checkOut,
leaveTypeStr,
system->records[i].remarks);
found = 1;
}
}
if(!found) {
printf("No attendance records for this time period!\n");
}
}
// Generate attendance report
void generateReport(AttendanceSystem* system) {
char startDate[11], endDate[11];
printf("Please enter start date (YYYY-MM-DD): ");
scanf("%s", startDate);
printf("Please enter end date (YYYY-MM-DD): ");
scanf("%s", endDate);
printf("\nAttendance Report (%s to %s):\n", startDate, endDate);
printf("Employee ID\tEmployee Name\tDepartment\tAttendance Days\tLate Days\tEarly Leave Days\tLeave Days\n");
for(int i = 0; i < system->employeeCount; i++) {
int attendanceDays = 0;
int lateDays = 0;
int earlyLeaveDays = 0;
int leaveDays = 0;
for(int j = 0; j < system->recordCount; j++) {
if(system->records[j].employeeId == system->employees[i].id &&
strcmp(system->records[j].date, startDate) >= 0 &&
strcmp(system->records[j].date, endDate) <= 0) {
if(system->records[j].leaveType > 0) {
leaveDays++;
} else if(strcmp(system->records[j].checkIn, "00:00") != 0 &&
strcmp(system->records[j].checkOut, "00:00") != 0) {
attendanceDays++;
// Simple late check (assume late if after 9:00)
if(strcmp(system->records[j].checkIn, "09:00") > 0) {
lateDays++;
}
// Simple early leave check (assume early if before 17:00)
if(strcmp(system->records[j].checkOut, "17:00") < 0) {
earlyLeaveDays++;
}
}
}
}
printf("%d\t%s\t\t%s\t\t%d\t\t%d\t\t%d\t\t%d\n",
system->employees[i].id,
system->employees[i].name,
system->employees[i].department,
attendanceDays,
lateDays,
earlyLeaveDays,
leaveDays);
}
}
// Save data to file
void saveData(AttendanceSystem* system) {
FILE* empFile = fopen("employees.dat", "wb");
FILE* recFile = fopen("records.dat", "wb");
if(empFile) {
fwrite(&system->employeeCount, sizeof(int), 1, empFile);
fwrite(system->employees, sizeof(Employee), system->employeeCount, empFile);
fclose(empFile);
}
if(recFile) {
fwrite(&system->recordCount, sizeof(int), 1, recFile);
fwrite(system->records, sizeof(AttendanceRecord), system->recordCount, recFile);
fclose(recFile);
}
printf("Data has been saved!\n");
}
// Load data from file
void loadData(AttendanceSystem* system) {
FILE* empFile = fopen("employees.dat", "rb");
FILE* recFile = fopen("records.dat", "rb");
if(empFile) {
fread(&system->employeeCount, sizeof(int), 1, empFile);
fread(system->employees, sizeof(Employee), system->employeeCount, empFile);
fclose(empFile);
}
if(recFile) {
fread(&system->recordCount, sizeof(int), 1, recFile);
fread(system->records, sizeof(AttendanceRecord), system->recordCount, recFile);
fclose(recFile);
}
}
// Find employee by ID
int findEmployeeById(AttendanceSystem* system, int id) {
for(int i = 0; i < system->employeeCount; i++) {
if(system->employees[i].id == id) {
return i;
}
}
return -1;
}
// Find attendance record
int findRecord(AttendanceSystem* system, int employeeId, const char* date) {
for(int i = 0; i < system->recordCount; i++) {
if(system->records[i].employeeId == employeeId &&
strcmp(system->records[i].date, date) == 0) {
return i;
}
}
return -1;
}
// Get current date
void getCurrentDate(char* date) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(date, "%04d-%02d-%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
}
// Get current time
void getCurrentTime(char* ctime) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(ctime, "%02d:%02d", tm.tm_hour, tm.tm_min);
}
Testing

D:\CODING\cyyzwsq\EmployeeSM\cmake-build-debug\EmployeeSM.exe
Welcome to the Employee Attendance Management System!
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:1
Please enter employee ID:1001
Please enter employee name:Zhang San
Please enter employee department:IT
Employee added successfully!
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:1
Please enter employee ID:1002
Please enter employee name:Li Si
Please enter employee department:Sales
Employee added successfully!
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:5
Please enter employee ID:1001
Please enter start date (YYYY-MM-DD):2025-08-01
Please enter end date (YYYY-MM-DD):2025-09-08
Zhang San's attendance records (2025-08-01 to 2025-09-08):
Date Check-in Time Check-out Time Leave Type Remarks
No attendance records for this time period!
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:2
Please enter employee ID:1001
Check-in successful! Time: 2025-09-08 20:56
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:2
Please enter employee ID:1002
Check-in successful! Time: 2025-09-08 20:56
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:6
Please enter start date (YYYY-MM-DD):2025-09-01
Please enter end date (YYYY-MM-DD):2025-09-08
Attendance Report (2025-09-01 to 2025-09-08):
Employee ID Employee Name Department Attendance Days Late Days Early Leave Days Leave Days
1001 Zhang San IT 0 0 0 0
1002 Li Si Sales 0 0 0 0
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:3
Please enter employee ID:1001
Check-out successful! Time: 2025-09-08 20:58
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:3
Please enter employee ID:1002
Check-out successful! Time: 2025-09-08 20:58
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:6
Please enter start date (YYYY-MM-DD):2025-09-01
Please enter end date (YYYY-MM-DD):2025-09-08
Attendance Report (2025-09-01 to 2025-09-08):
Employee ID Employee Name Department Attendance Days Late Days Early Leave Days Leave Days
1001 Zhang San IT 1 1 0 0
1002 Li Si Sales 1 1 0 0
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:4
Please enter employee ID:1001
Please enter leave date (YYYY-MM-DD):2025-09-08
Please select leave type (1. Personal Leave 2. Sick Leave 3. Annual Leave):3
Please enter leave reason:Urgent matter
Leave application submitted successfully!
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:6
Please enter start date (YYYY-MM-DD):2025-09-01
Please enter end date (YYYY-MM-DD):2025-09-08
Attendance Report (2025-09-01 to 2025-09-08):
Employee ID Employee Name Department Attendance Days Late Days Early Leave Days Leave Days
1001 Zhang San IT 0 0 0 1
1002 Li Si Sales 1 1 0 0
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:5
Please enter employee ID:1001
Please enter start date (YYYY-MM-DD):2025-09-01
Please enter end date (YYYY-MM-DD):2025-09-08
Zhang San's attendance records (2025-09-01 to 2025-09-08):
Date Check-in Time Check-out Time Leave Type Remarks
2025-09-08 20:56 20:58 Annual Leave Urgent matter
===== Employee Attendance Management System =====
1. Add Employee
2. Check-in
3. Check-out
4. Leave
5. Query Attendance Records
6. Generate Attendance Report
0. Exit System
============================
Please select an operation:
Extension Suggestions
- A permission management system can be added to distinguish between administrators and regular employees
- More complex attendance rules can be added, such as flexible working hours, overtime calculations, etc.
- A data export function can be added to support exporting reports in Excel or PDF format
- Network functionality can be added to implement multi-terminal attendance records
This system provides basic attendance management functions and can be further expanded and optimized according to actual needs.
--End--
If you have read this far, it means you like the articles from this public account. Welcome to pin (star) this public account C Language Chinese Community, so you can get push notifications as soon as possible~
In this public account, reply: 1024, to receive a free C language learning gift package
If you think the content is good, please give me a "like".