Introduction
In this tutorial, we will create a simple employee database using the C programming language.
We will implement basic CRUD (Create, Read, Update, Delete) operations to manage employee information.
This program will allow us to add new employee records, display all employees, modify existing records,
and remove employee data from the database.
Objective
The objective of this program is to understand how to handle basic database operations like creating,
reading, updating, and deleting employee records using C programming. This program is a foundation
for learning more advanced database management techniques.
Code: Employee Database in C
#include
#include
// Define a structure for storing employee information
struct Employee {
int id;
char name[100];
float salary;
};
// Declare an array to hold employees
struct Employee employees[100];
int employee_count = 0;
// Function prototypes
void add_employee();
void view_employees();
void update_employee();
void delete_employee();
int main() {
int choice;
while(1) {
printf("\nEmployee Database Menu:\n");
printf("1. Add Employee\n");
printf("2. View Employees\n");
printf("3. Update Employee\n");
printf("4. Delete Employee\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_employee();
break;
case 2:
view_employees();
break;
case 3:
update_employee();
break;
case 4:
delete_employee();
break;
case 5:
printf("Exiting program...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
// Function to add a new employee
void add_employee() {
struct Employee new_employee;
printf("Enter Employee ID: ");
scanf("%d", &new_employee.id);
getchar(); // Clear newline character from buffer
printf("Enter Employee Name: ");
fgets(new_employee.name, sizeof(new_employee.name), stdin);
new_employee.name[strcspn(new_employee.name, "\n")] = 0; // Remove newline
printf("Enter Employee Salary: ");
scanf("%f", &new_employee.salary);
employees[employee_count++] = new_employee;
printf("Employee added successfully!\n");
}
// Function to view all employees
void view_employees() {
if (employee_count == 0) {
printf("No employee records found.\n");
return;
}
printf("\nEmployee Records:\n");
for (int i = 0; i < employee_count; i++) {
printf("ID: %d, Name: %s, Salary: %.2f\n", employees[i].id, employees[i].name, employees[i].salary);
}
}
// Function to update employee details
void update_employee() {
int id, found = 0;
printf("Enter Employee ID to update: ");
scanf("%d", &id);
for (int i = 0; i < employee_count; i++) {
if (employees[i].id == id) {
found = 1;
printf("Enter new name: ");
getchar(); // Clear newline character from buffer
fgets(employees[i].name, sizeof(employees[i].name), stdin);
employees[i].name[strcspn(employees[i].name, "\n")] = 0; // Remove newline
printf("Enter new salary: ");
scanf("%f", &employees[i].salary);
printf("Employee updated successfully!\n");
break;
}
}
if (!found) {
printf("Employee with ID %d not found.\n", id);
}
}
// Function to delete an employee
void delete_employee() {
int id, found = 0;
printf("Enter Employee ID to delete: ");
scanf("%d", &id);
for (int i = 0; i < employee_count; i++) {
if (employees[i].id == id) {
found = 1;
for (int j = i; j < employee_count - 1; j++) {
employees[j] = employees[j + 1]; // Shift elements left
}
employee_count--;
printf("Employee deleted successfully!\n");
break;
}
}
if (!found) {
printf("Employee with ID %d not found.\n", id);
}
}
Explanation of the Program
This program creates an employee database using a C structure to hold employee data,
and provides four key functions to perform CRUD operations:
- add_employee: Adds a new employee record by taking input from the user (ID, name, and salary).
- view_employees: Displays all the employee records stored in the database.
- update_employee: Allows the user to update an employee’s details using their ID.
- delete_employee: Removes an employee record from the database using their ID.
The program runs a loop until the user selects the option to exit. It provides a simple text-based
menu for interacting with the database.
How to Run the Program
1. Copy the provided code into a text file and save it with a “.c” extension (e.g., employee_database.c).
2. Compile the code using a C compiler (e.g., GCC). If you’re using GCC, run the following command in your terminal:
gcc employee_database.c -o employee_database
3. After successful compilation, run the program using the command:
./employee_database
4. Follow the on-screen prompts to perform CRUD operations on the employee database.

