Problem: Write a C program to read and print employee details using structure.

To store multiple employee details we will use an array of structures. Each element in the array will represent a single employee.

Each Structure i.e. Employee contains:

  • Name
  • Id
  • Salary

In our program, we will read the inputs for each employee from the user, and then output all employee details by iterating through the array using the ‘for’ loop.

C Program to Read and Print Employee Details

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    char name[30];
    int id;
    double salary;
} Employee;
 
int main()
{
    //number of employees
    int n=2;

    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n",i+1);

        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);

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

        //Salary
        printf("Salary: ");
        scanf("%lf",&employees[i].salary);

        //to consume extra '\n' input
        char ch = getchar();
 
        printf("\n");
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
        printf("Name \t: ");
        printf("%s \n",employees[i].name);
 
        printf("Id \t: ");
        printf("%d \n",employees[i].id);
 
        printf("Salary \t: ");
        printf("%.2lf \n",employees[i].salary);
 
        printf("\n");
    }
 
    return 0;
}

The output of the Program:

C Program for Employee Details using Structure

Calculate Salary of the Employee from Basic

In the above program, we are directly asking the user to input the salary of every employee.

In case we need to calculate the salary from the basic, we will have to add an extra variable (i.e. basic_salary) to the structure and calculate the net_salary using the formula:  net_salary = basic_salary + HRA + DA – PF

Where,

  • HRA is 10% of basic salary i.e., HRA=basic_salary*0.1;
  • DA is 5% of basic salary i.e., DA=basic_salary*0.05;
  • PF is 12% of basic salary i.e., PF=basic_salary*0.12;

Putting them in the formula: net_salary = basic_salary * (1+ 0.1 + 0.05 – 0.12).

If we modify the above program then the code will be:

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    
    char name[30];
    int id;
    double basic_salary;
    double net_salary;
    
} Employee;
 
int main()
{
    //number of employees
    int n=2;
    
    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n", (i+1));
        
        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);
        
        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);
        
        //Salary
        printf("Basic Salary: ");
        scanf("%lf",&employees[i].basic_salary);
        
        //to consume extra '\n' input
        char ch = getchar();
        
        employees[i].net_salary = employees[i].basic_salary*1.03;
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
 
        printf("Name \t: ");
        printf("%s \n",employees[i].name);
 
        printf("Id \t: ");
        printf("%d \n",employees[i].id);
 
        printf("Salary \t: ");
        printf("%.2lf \n",employees[i].net_salary);
 
        printf("\n");
    }
 
    return 0;
}

If you didn’t understand any part of the code then do comment below.

This Post Has 20 Comments

  1. Balaji

    What is the difference between array and structure array

    1. Adarsh Kumar

      They both are similar. The difference is the type of data in the array. When we declare int array[10], we mean to store integers into the array and in case of Employee array[10], we mean to store structure objects into the array.

  2. Balaji

    Why you have write the Employee; after defining structure

    1. Adarsh Kumar

      Any name you put at the end of the structure declaration along with typedef at the beginning is used to rename the structure. In our case, we have renamed the structure as Employee. so that we can address the structure in main funtion using just Employee instead of struct Employee.

  3. Shaik Amin

    Write a program to read the details of ‘N’ employees (ID, Name, Salary) and display the details of those employees whose salary is above Rs.10,000/-.
    Can any one write this program.?

    1. Adarsh Kumar

      The logic of your problem can be easily solved using the above program. Just include if(employees[i].salary> 10000) indide the loop which prints the employee details.

        for(i=0; i 10000){
              printf("Name \t: ");
              printf("%s \n",employees[i].name);
       
              printf("Id \t: ");
              printf("%d \n",employees[i].id);
       
              printf("Salary \t: ");
              printf("%d \n",employees[i].salary);
       
              printf("\n");
            }
          }
      
  4. Rekha

    1. Write a Menu Driven C program to create an employee database using structure and perform following operations: (I) Accept Employee Record (II) Display All Employees Records (III) Search Employee Record by name and Display (IV) Search Employee Record by Employee ID and Display (V) Update Employee Record (Note: Employee record will have following fields Employee ID, Employee Name, Employee Department, Employee Salary)

    can i pls get a code for this

  5. Ritik vani

    How to add date of joining of the employee in the program

    1. Adarsh Kumar

      Add another char array (string) to store the date of joining.

      typedef struct{
       
          char name[30];
          int id;
          int salary;
          char dateOfJoining[20];
       
      } Employee;
      
  6. Jayashree

    In case of storing 20 employees,we need to change ?

    1. Adarsh Kumar

      change the value of n

      n=20
  7. Amira osama

    if I want to print details of one employee by his id what should I do?

    1. Adarsh Kumar

      If you want to print an employee by his id use any of the search algorithms such as linear search or binary search to search an employee in the employee array. If found then output his details.

      int id;
      scanf("%d", &id);  
      for(int i=0; i<n; i++){
          
          if(employees[i].id == id){
              printf("Name \t: ");
              printf("%s \n",employees[i].name);
       
              printf("Id \t: ");
              printf("%d \n",employees[i].id);
       
              printf("Salary \t: ");
              printf("%d \n",employees[i].salary);
          }
          
      }
      
  8. Sakshi

    Hi Adarsh, could you please tell me how can I compute total salary outgo for a month . For an employee using structures in C.
    Thanks in advance .

    1. Adarsh Kumar

      Updated the post with the solution.

  9. Sakshi

    Thank you so much for the reply Adarsh . This has helped me .

  10. Sakshi

    Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month.

    Could you please provide a solution for this pleasee?

  11. Sakshi

    The above program is giving error as ” array subscipt is not an integer” kindly suggest.

    1. Adarsh Kumar

      From my side, the code is working fine. Maybe you are trying to input the full name of the employee. Can you re-check, I have modified the code.

  12. Rohan

    Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month ??

    can you please share code for the same .

Leave a Reply