BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program for employee salary calculation using structure

Last Updated: May 14, 2024 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, we will write a C program to calculate the total salary of employees using structure.

Program for salary calculation using structure

The explanation of this is at the end of the program along with the sample output.

#include <stdio.h>

// Define the structure for employee details
struct Employee {
int id; // Employee ID
char name[50]; // Employee Name
float basic_salary; // Employee Basic Salary
float bonus; // Employee Bonus
float total_salary; // Employee Total Salary (Basic + Bonus)
};

// Function to calculate total salary
void calculateTotalSalary(struct Employee *emp) {
emp->total_salary = emp->basic_salary + emp->bonus;
}

int main() {
int num_employees, i;

// Ask user for the number of employees
printf("Enter the number of employees: ");
scanf("%d", &num_employees);

// Declare an array of structures to hold details for multiple employees
struct Employee employees[num_employees];

// Taking input for each employee
for (i = 0; i < num_employees; i++) {
printf("\nEnter details for employee %d:\n", i + 1);

printf("Enter Employee ID: ");
scanf("%d", &employees[i].id);

printf("Enter Employee Name: ");
scanf("%s", employees[i].name);

printf("Enter Employee Basic Salary: ");
scanf("%f", &employees[i].basic_salary);

printf("Enter Employee Bonus: ");
scanf("%f", &employees[i].bonus);

// Calculate the total salary for the current employee
calculateTotalSalary(&employees[i]);
}

// Displaying the employee details along with the total salary
printf("\nEmployee Details:\n");
for (i = 0; i < num_employees; i++) {
printf("\nDetails of Employee %d:\n", i + 1);
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Basic Salary: %.2f\n", employees[i].basic_salary);
printf("Bonus: %.2f\n", employees[i].bonus);
printf("Total Salary: %.2f\n", employees[i].total_salary);
}

return 0; // Indicate that the program ended successfully
}

How this program works?

Include Header File: The following line includes the standard input-output library which is necessary for using printf and scanf functions.

    #include <stdio.h>

      Structure Definition:

      struct Employee {
          int id;            // Employee ID
          char name[50];     // Employee Name
          float basic_salary; // Employee Basic Salary
          float bonus;       // Employee Bonus
          float total_salary; // Employee Total Salary (Basic + Bonus)
      };

      This defines a structure named Employee with five fields: id (integer), name (character array of size 50), basic_salary (float), bonus (float), and total_salary (float).

      Function to Calculate Total Salary:

      void calculateTotalSalary(struct Employee *emp) {
      emp->total_salary = emp->basic_salary + emp->bonus;
      }

      This function calculates the total salary by adding the basic salary and bonus, and stores the result in the total_salary field of the Employee structure using emp variable.

      Main Function:

      int main() {
      int num_employees, i;

      // Ask user for the number of employees
      printf("Enter the number of employees: ");
      scanf("%d", &num_employees);

      // Declare an array of structures to hold details for multiple employees
      struct Employee employees[num_employees];

      In the main function, we have a variable num_employees to store the number of employees and an array of Employee structures to store details for each employee.

      Taking Input:

      for (i = 0; i < num_employees; i++) {
      printf("\nEnter details for employee %d:\n", i + 1);

      printf("Enter Employee ID: ");
      scanf("%d", &employees[i].id);

      printf("Enter Employee Name: ");
      scanf("%s", employees[i].name);

      printf("Enter Employee Basic Salary: ");
      scanf("%f", &employees[i].basic_salary);

      printf("Enter Employee Bonus: ");
      scanf("%f", &employees[i].bonus);

      // Calculate the total salary for the current employee
      calculateTotalSalary(&employees[i]);
      }

      Here, we are taking employee details from the user and calling function calculateTotalSalary to calculate the total salary of each employee.

      Displaying Details:

      printf("\nEmployee Details:\n");
      for (i = 0; i < num_employees; i++) {
      printf("\nDetails of Employee %d:\n", i + 1);
      printf("ID: %d\n", employees[i].id);
      printf("Name: %s\n", employees[i].name);
      printf("Basic Salary: %.2f\n", employees[i].basic_salary);
      printf("Bonus: %.2f\n", employees[i].bonus);
      printf("Total Salary: %.2f\n", employees[i].total_salary);
      }

      Displaying details of each employee, including the calculated total salary.

      Return Statement:

      return 0; // Indicate that the program ended successfully

      This line indicates that the program has ended successfully by returning 0 from the main function.

      Output:

      Enter the number of employees: 2

      Enter details for employee 1:
      Enter Employee ID: 1001
      Enter Employee Name: Chaitanya
      Enter Employee Basic Salary: 55000
      Enter Employee Bonus: 5000

      Enter details for employee 2:
      Enter Employee ID: 1002
      Enter Employee Name: Rahul
      Enter Employee Basic Salary: 64000
      Enter Employee Bonus: 6000

      Employee Details:

      Details of Employee 1:
      ID: 1001
      Name: Chaitanya
      Basic Salary: 55000.00
      Bonus: 5000.00
      Total Salary: 60000.00

      Details of Employee 2:
      ID: 1002
      Name: Rahul
      Basic Salary: 64000.00
      Bonus: 6000.00
      Total Salary: 70000.00

      Top Related Articles:

      1. C Program to Print Pyramid Star Pattern
      2. C Program to Read the First Line From a File
      3. C Program to calculate Area of an Equilateral triangle
      4. C Program to concatenate two strings without using strcat
      5. C Program to Add two numbers

      About the Author

      I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

      – Chaitanya

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap