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 employees using structure in C.

This program will take the employee details as inputs from the user, store them by using a predefined structure and print it out.

With this program, you will get a good understanding of structure in C.

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 for an employee.
  • Define variables of this structure.
  • Ask the user to enter the details one by one and store them in the variable details.
  • Print out the content.

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

Below is the complete C program:

#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;
}

Here,

  • Employee is a structure we have defined to hold the employee details. This structure can hold the id, age, salary, name, designation and department values.
  • Inside main, we are creating a Employee variable e. We will read the data and store it in the fields of this variable.
  • Using printf, scanf and fgets, we are reading the data and storing these in the fields of the variable e.

    • You can use dot . to access a property in a structure.
  • Once the reading is done, we are printing the content, i.e. the details of the employee.

Output:

If you run this program, it will print output as like 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: C program to store and display the employee details using structure and methods:

We can also create different methods to do the data reading part and data printing part.

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;
}
  • We have created two new methods here. One is to read the data and another is to print the data.
  • readEmployeeDetails 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 readEmployeeDetails to e in 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 main and it will give us similar output as the previous example.

You might also like: