C program to store and display the details of an employee by using structure

C program to store and display the details of an employee by using structure:

In this post, we will learn how to store and display the details of employee data using structures in C. This program will take the employee details as inputs from the user, store them by using a predefined structure and print them out.

Algorithm to use:

We will use the below algorithm for this program:

  • Define one structure to hold the details of an employee. This structure can hold the ID, name, age, designation, department and salary of an employee. You can also add more values to hold. I am considering only these 6 values for this tutorial.
  • Define multiple variables of this structure. The variable type will be similar to the defined structure.
  • Ask the user to enter the details of employees and assign these values to the structure variables.
  • Print out the values of the variables.

Method 1: C program to store and display the employee details using structure:

Below is the complete C program that uses the above algorithm:

#include <stdio.h>

struct Employee
{
    int id, age, salary;
    char name[30], designation[30], department[30];
};

int main()
{
    struct Employee e;

    printf("Enter the id of the Employee: ");
    scanf("%d", &e.id);

    printf("Enter the age of the Employee: ");
    scanf("%d", &e.age);

    printf("Enter the name of the Employee: ");
    getchar();
    fgets(e.name, 30, stdin);

    printf("Enter the designation of the Employee: ");
    fgets(e.designation, 30, stdin);

    printf("Enter the department of the Employee: ");
    fgets(e.department, 30, stdin);

    printf("Enter the salary of the Employee: ");
    scanf("%d", &e.salary);

    printf("\nEmployee Details:\n");
    printf("Employee Id: %d\n", e.id);
    printf("Employee Name: %s", e.name);
    printf("Employee age: %d\n", e.age);
    printf("Employee designation: %s", e.designation);
    printf("Employee department: %s", e.department);
    printf("Employee salary: %d\n", e.salary);

    return 0;
}

Download it on Github

  • We have created one structure Employee and declared its member variables. We can use the struct keyword to create a structure. This structure has three integer member variables and three character array member variables.
  • Inside the main method, we have created a structure variable e. We will read the data and assign it to the members of this variable.
  • By using the printf, scanf_ and fgets methods, we are reading the data and assigning these values to the members of the structure variable e.
    • We can use the dot . to access struct members.
  • Once the values are entered, it prints the contents, i.e. the details of the employee.

Output:

If you run this program, it will print outputs as below:

Enter the id of the Employee: 1231
Enter the age of the Employee: 21
Enter the name of the Employee: James Smith
Enter the designation of the Employee: Manager
Enter the department of the Employee: Dept-A
Enter the salary of the Employee: 120000

Employee Details:
Employee Id: 1231
Employee Name: James Smith
Employee age: 21
Employee designation: Manager
Employee department: Dept-A
Employee salary: 120000

C store and display employee details using structure

Method 2: How to use a separate method to read and print employee data with structure:

We can also create different methods to read and print the structure data:

For example,

#include <stdio.h>

struct Employee
{
    int id, age, salary;
    char name[30], designation[30], department[30];
};

struct Employee readEmployeeDetails(struct Employee e)
{
    printf("Enter the id of the Employee: ");
    scanf("%d", &e.id);

    printf("Enter the age of the Employee: ");
    scanf("%d", &e.age);

    printf("Enter the name of the Employee: ");
    getchar();
    fgets(e.name, 30, stdin);

    printf("Enter the designation of the Employee: ");
    fgets(e.designation, 30, stdin);

    printf("Enter the department of the Employee: ");
    fgets(e.department, 30, stdin);

    printf("Enter the salary of the Employee: ");
    scanf("%d", &e.salary);

    return e;
}

void printEmployeeDetails(struct Employee e)
{
    printf("\nEmployee Details:\n");
    printf("Employee Id: %d\n", e.id);
    printf("Employee Name: %s", e.name);
    printf("Employee age: %d\n", e.age);
    printf("Employee designation: %s", e.designation);
    printf("Employee department: %s", e.department);
    printf("Employee salary: %d\n", e.salary);
}
int main()
{
    struct Employee e;

    e = readEmployeeDetails(e);

    printEmployeeDetails(e);

    return 0;
}

Download it on Github

  • We have created two new methods here. One is to read the data and another one is to print the data.
  • The readEmployeeDetails method takes one structure of type Employee as the parameter. It reads the data from the user and assigns these values to the structure variables. It returns the same structure back.
  • We are assigning the value returned by the readEmployeeDetails to e inside the main method.
  • The printEmployeeDetails method takes one structure and prints the content of the structure. This structure should be of type Employee.

We are calling only these two methods inside the main method and it will give similar output as the previous example.

You might also like: