Employee Payroll System in C
Every company pays salaries, and every salary is a small calculation: a basic amount, plus allowances like HRA and DA, minus deductions like provident fund and income tax. A payroll system does this for every employee and prints payslips and a monthly report.
In this final project you will store employees in a file, calculate gross and net pay with clear rules, apply slab-based income tax, print a payslip and a payroll report, and learn the useful pattern of loading a whole file into an array.
What You Will Learn
- How to split data into stored values and calculated values.
- How to calculate allowances, gross and net salary.
- How slab-based income tax works in code.
- How to return several results from a function using a structure.
- How to load a file into an array and save it back.
- How to print aligned payslips and reports.
Employee Payroll System in C Explained
Exam definition: A payroll system calculates each employee's pay from stored data. Gross salary = basic + allowances (e.g. HRA, DA); net salary = gross − deductions (e.g. PF, income tax). Slab-based tax taxes each portion of income at the rate of the slab it falls in. A function can return all the computed components at once by returning a structure.
Memory trick: A salary is a staircase: walk up with earnings (basic, HRA, DA) to reach the gross, then walk down with deductions (PF, tax) to reach the net. Tax slabs are water tanks filled one after another: only the water in each tank is charged at that tank's rate.
Syntax: Returning Several Values with a Structure
struct pay { double hra, da, gross, pf, tax, net; };
struct pay calculate(const struct employee *e)
{
struct pay p;
p.hra = e->basic * 0.20;
p.da = e->basic * 0.10;
p.gross = e->basic + p.hra + p.da;
p.pf = e->basic * 0.12;
p.tax = yearly_tax(p.gross * 12) / 12;
p.net = p.gross - p.pf - p.tax;
return p; /* all six values at once */
}
| Yearly income slab | Tax rate on that part (this project) |
|---|---|
| Up to 3,00,000 | 0% |
| 3,00,001 – 6,00,000 | 5% |
| 6,00,001 – 9,00,000 | 10% |
| Above 9,00,000 | 15% |
Example 1: Employee Payroll System
#include <stdio.h>
#define MAX 50
#define FILE_NAME "employees.dat"
struct employee
{
int id;
char name[30];
double basic;
};
struct pay
{
double hra, da, gross, pf, tax, net;
};
struct employee emp[MAX];
int count = 0;
/* yearly tax on yearly income (simple slabs) */
double yearly_tax(double income)
{
double tax = 0;
if (income > 900000) { tax += (income - 900000) * 0.15; income = 900000; }
if (income > 600000) { tax += (income - 600000) * 0.10; income = 600000; }
if (income > 300000) { tax += (income - 300000) * 0.05; }
return tax;
}
struct pay calculate(const struct employee *e)
{
struct pay p;
p.hra = e->basic * 0.20;
p.da = e->basic * 0.10;
p.gross = e->basic + p.hra + p.da;
p.pf = e->basic * 0.12;
p.tax = yearly_tax(p.gross * 12) / 12;
p.net = p.gross - p.pf - p.tax;
return p;
}
void load(void)
{
FILE *fp = fopen(FILE_NAME, "rb");
if (fp == NULL) return;
count = (int) fread(emp, sizeof emp[0], MAX, fp);
fclose(fp);
}
void save(void)
{
FILE *fp = fopen(FILE_NAME, "wb");
if (fp == NULL) return;
fwrite(emp, sizeof emp[0], count, fp);
fclose(fp);
}
void add_employee(void)
{
struct employee e;
if (count == MAX) { printf("Employee list is full\n"); return; }
e.id = count == 0 ? 1 : emp[count - 1].id + 1;
printf("Name: ");
scanf(" %29[^\n]", e.name);
printf("Basic salary: ");
if (scanf("%lf", &e.basic) != 1 || e.basic <= 0) { printf("Invalid salary\n"); return; }
emp[count++] = e;
save();
printf("Employee %d added\n", e.id);
}
void payslip(void)
{
int id, i;
printf("Employee ID: ");
if (scanf("%d", &id) != 1) return;
for (i = 0; i < count; i++)
if (emp[i].id == id)
{
struct pay p = calculate(&emp[i]);
printf("----- PAYSLIP -----\n");
printf("%-12s %s (ID %d)\n", "Employee", emp[i].name, emp[i].id);
printf("%-12s %10.2f\n", "Basic", emp[i].basic);
printf("%-12s %10.2f\n", "HRA (20%)", p.hra);
printf("%-12s %10.2f\n", "DA (10%)", p.da);
printf("%-12s %10.2f\n", "Gross", p.gross);
printf("%-12s %10.2f\n", "PF (12%)", p.pf);
printf("%-12s %10.2f\n", "Income tax", p.tax);
printf("%-12s %10.2f\n", "NET PAY", p.net);
return;
}
printf("No employee with that ID\n");
}
void payroll_report(void)
{
int i;
double total = 0;
printf("%-3s %-14s %10s %10s\n", "ID", "Name", "Gross", "Net");
for (i = 0; i < count; i++)
{
struct pay p = calculate(&emp[i]);
printf("%-3d %-14s %10.2f %10.2f\n", emp[i].id, emp[i].name, p.gross, p.net);
total += p.net;
}
printf("Total monthly payout: %.2f\n", total);
}
int main(void)
{
int choice;
load();
while (1)
{
printf("\n1.Add 2.Payslip 3.Report 0.Exit\nChoice: ");
if (scanf("%d", &choice) != 1 || choice == 0)
break;
switch (choice)
{
case 1: add_employee(); break;
case 2: payslip(); break;
case 3: payroll_report(); break;
default: printf("Invalid choice\n");
}
}
printf("Payroll closed.\n");
return 0;
}
Output
1.Add 2.Payslip 3.Report 0.Exit Choice: 1 Name: Anjali Gupta Basic salary: 30000 Employee 1 added 1.Add 2.Payslip 3.Report 0.Exit Choice: 1 Name: Suresh Nair Basic salary: 60000 Employee 2 added 1.Add 2.Payslip 3.Report 0.Exit Choice: 1 Name: Pooja Singh Basic salary: 18000 Employee 3 added 1.Add 2.Payslip 3.Report 0.Exit Choice: 2 Employee ID: 2 ----- PAYSLIP ----- Employee Suresh Nair (ID 2) Basic 60000.00 HRA (20%) 12000.00 DA (10%) 6000.00 Gross 78000.00 PF (12%) 7200.00 Income tax 4200.00 NET PAY 66600.00 1.Add 2.Payslip 3.Report 0.Exit Choice: 3 ID Name Gross Net 1 Anjali Gupta 39000.00 34700.00 2 Suresh Nair 78000.00 66600.00 3 Pooja Singh 23400.00 21240.00 Total monthly payout: 122540.00 1.Add 2.Payslip 3.Report 0.Exit Choice: 2 Employee ID: 7 No employee with that ID 1.Add 2.Payslip 3.Report 0.Exit Choice: 0 Payroll closed.
Code Explanation
struct employeestores only what doesn't change every month (id, name, basic).struct payholds the calculated parts. Calculated values are not stored; they are computed when needed, so a change of rules updates every payslip.calculate()applies the salary rules: HRA 20% and DA 10% of basic make the gross; PF (12% of basic) and income tax are deductions; net = gross − deductions.yearly_tax()applies slabs from the top down: the part above 9 lakh at 15%, the part between 6 and 9 lakh at 10%, and the part between 3 and 6 lakh at 5%. Suresh earns 9,36,000 a year: 36,000 × 15% + 3,00,000 × 10% + 3,00,000 × 5% = 50,400, which is 4,200 a month.- All employees are kept in an array while the program runs.
load()reads the whole file into the array at the start;save()writes the whole array after every change. For small data this is simpler than updating records one by one. - New IDs are the last ID + 1, so they never repeat.
- The report reuses
calculate(), so the payslip and the report always agree.
How the Salary Is Calculated
Basic 30,000
+ HRA (20% of basic) 6,000
+ DA (10% of basic) 3,000
= GROSS 39,000
- PF (12% of basic) 3,600
- Tax (yearly slab / 12) 700 yearly gross 4,68,000: (4,68,000 - 3,00,000) x 5% = 8,400
= NET PAY 34,700
These percentages and slabs are simplified for learning; real payroll follows the current tax rules and company policy. Because every rule lives in one function, changing a rule means editing one place.
Example 2: Tax Slab Table
#include <stdio.h>
double yearly_tax(double income)
{
double tax = 0;
if (income > 900000) { tax += (income - 900000) * 0.15; income = 900000; }
if (income > 600000) { tax += (income - 600000) * 0.10; income = 600000; }
if (income > 300000) { tax += (income - 300000) * 0.05; }
return tax;
}
int main(void)
{
double incomes[] = {250000, 300000, 450000, 600000, 750000, 1200000};
int i;
printf("%12s %10s %8s\n", "Income", "Tax", "Rate");
for (i = 0; i < 6; i++)
{
double t = yearly_tax(incomes[i]);
printf("%12.0f %10.0f %7.2f%%\n", incomes[i], t, t * 100 / incomes[i]);
}
return 0;
}
Output
Income Tax Rate
250000 0 0.00%
300000 0 0.00%
450000 7500 1.67%
600000 15000 2.50%
750000 30000 4.00%
1200000 90000 7.50%
- Only the part of income inside each slab is taxed at that slab's rate. Earning 6,00,001 doesn't make the whole income taxed at 10%.
- 12 lakh: 3 lakh × 15% + 3 lakh × 10% + 3 lakh × 5% = 45,000 + 30,000 + 15,000 = 90,000.
- The last column is the effective rate (tax ÷ income), always lower than the top slab rate.
Important Points to Remember
- Gross = basic + allowances (HRA, DA); net = gross − deductions (PF, tax).
- Store input data (basic); compute derived values (gross, net) when needed.
- Put each rule in one function (calculate, yearly_tax) and reuse it.
- Slab tax: tax each part of income at its own rate, from the top slab down.
- Small datasets: load the whole file into an array at start, save the array after changes.
- Check array limits (
count == MAX) and validate input (salary > 0).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Taxing the whole income at the top rate | Tax far too high | Tax each slab part separately |
| Computing PF on gross instead of basic | Wrong deduction | Follow the rule exactly (12% of basic here) |
| Storing gross and net in the file | Old values stay when rules change | Store basic; calculate the rest |
| Applying a yearly tax to a monthly salary | Tax 12 times too small or large | Convert: yearly = monthly × 12, then ÷ 12 |
| No check for a full array | Writes past the end of emp[] | if (count == MAX) refuse |
Employee Payroll System Practice Questions
- What is the difference between gross and net salary?
- Calculate HRA, DA, gross and PF for a basic salary of 20,000 using this project's rules.
- Why are gross and net not stored in the file?
- Explain how slab-based tax is calculated with an example.
- What do the
load()andsave()functions do?
Show answers
- Gross is the total earnings (basic + allowances); net is what the employee receives after deductions.
- HRA 4,000; DA 2,000; gross 26,000; PF 2,400.
- They can be calculated from the basic salary; storing them would leave outdated values when rules change.
- Each part of the income is taxed at its slab's rate, e.g. 7.5 lakh: 1.5 lakh × 10% + 3 lakh × 5% = 15,000 + 15,000 = 30,000.
- load reads all employee records from the file into the array at start; save writes the whole array back to the file after each change.
Coding Exercise
Add overtime pay: an employee is paid 1.5 times the hourly rate for each overtime hour, where hourly rate = basic ÷ (26 days × 8 hours). Calculate overtime pay for a basic of 26,000 and 10, 20 and 0 overtime hours.
Show solution
#include <stdio.h>
double overtime_pay(double basic, int hours)
{
double hourly = basic / (26 * 8);
return hours * hourly * 1.5;
}
int main(void)
{
int hours[] = {10, 20, 0};
int i;
printf("Hourly rate for basic 26000: %.2f\n", 26000.0 / (26 * 8));
for (i = 0; i < 3; i++)
printf("%2d overtime hours -> Rs %.2f\n", hours[i], overtime_pay(26000, hours[i]));
return 0;
}
Hourly rate for basic 26000: 125.00 10 overtime hours -> Rs 1875.00 20 overtime hours -> Rs 3750.00 0 overtime hours -> Rs 0.00
Add it to calculate() as another earning to include it in the gross.
Quick Quiz: Employee Payroll System (MCQ)
-
Net salary is:
A) Basic + HRA B) Gross − deductions C) Gross + tax D) Basic only
-
With HRA 20% and DA 10%, the gross for basic 10,000 is:
A) 10,000 B) 12,000 C) 13,000 D) 13,200
-
Under this project's slabs, tax on a yearly income of 3,00,000 is:
A) 0 B) 15,000 C) 30,000 D) 5%
-
Which should be stored in the employee file?
A) Net pay B) Gross pay C) Basic salary D) Tax
-
fread(emp, sizeof emp[0], MAX, fp)returns:A) Bytes read B) Number of records read C) Always MAX D) 0 or 1
Show answers
- B: gross − deductions.
- C: 13,000.
- A: 0.
- C: basic salary.
- B: the number of records read.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Gross | Basic + HRA + DA |
|---|---|
| Deductions | PF + income tax |
| Net | Gross − deductions |
| Store | Input data (basic); compute the rest |
| Slab tax | Each part at its own rate |
| Small data | Load file → array, save array → file |
| fread count | Returns records read |
Frequently asked questions
- How is net salary calculated in a payroll program?
- Net = basic + allowances − deductions (PF, tax), with each rule in a function.
- How do you calculate slab-based income tax in C?
- Start from the top slab: tax the amount above each limit at that slab's rate, then lower the income to the limit and continue.
- Why keep salary rules in one function?
- So payslips and reports agree, and a rule change needs one edit.
Remember
- Earnings up, deductions down: gross then net.
- Store inputs, calculate outputs.
Next Topic
Next lesson: C Interview Questions for Freshers. Module 18 begins: the most asked C interview questions for freshers, with short answers.