C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures in C

Store employee details (salary, age, etc.)

C Program: Store and Display Employee Details

C

#include <stdio.h>

 

// Define structure for employee

struct Employee {

    int id;

    char name[100];

    int age;

    float salary;

    char department[50];

};

 

// Function declarations

void inputEmployees(struct Employee emp[], int n);

void displayEmployees(struct Employee emp[], int n);

 

int main() {

    struct Employee emp[100];

    int n;

 

    printf("Enter number of employees: ");

    scanf("%d", &n);

 

    // Input and display employee details

    inputEmployees(emp, n);

    displayEmployees(emp, n);

 

    return 0;

}

 

// Function to input employee details

void inputEmployees(struct Employee emp[], int n) {

    for (int i = 0; i < n; i++) {

        printf("\nEnter details of Employee %d\n", i + 1);

        printf("Enter Employee ID: ");

        scanf("%d", &emp[i].id);

        getchar(); // clear input buffer

 

        printf("Enter Name: ");

        gets(emp[i].name);

 

        printf("Enter Age: ");

        scanf("%d", &emp[i].age);

 

        printf("Enter Salary: ");

        scanf("%f", &emp[i].salary);

        getchar();

 

        printf("Enter Department: ");

        gets(emp[i].department);

    }

}

 

// Function to display employee details

void displayEmployees(struct Employee emp[], int n) {

    printf("\n===== EMPLOYEE DETAILS =====\n");

    for (int i = 0; i < n; i++) {

        printf("\nEmployee %d\n", i + 1);

        printf("Employee ID : %d\n", emp[i].id);

        printf("Name        : %s\n", emp[i].name);

        printf("Age         : %d\n", emp[i].age);

        printf("Salary (₹)  : %.2f\n", emp[i].salary);

        printf("Department  : %s\n", emp[i].department);

    }

}

Output

 
OUTPUT :
Enter number of employees: 2

Enter details of Employee 1
Enter Employee ID: 101
Enter Name: Rahul Sharma
Enter Age: 30
Enter Salary: 55000
Enter Department: IT

Enter details of Employee 2
Enter Employee ID: 102
Enter Name: Priya Mehta
Enter Age: 28
Enter Salary: 60000
Enter Department: HR

===== EMPLOYEE DETAILS =====

Employee 1
Employee ID : 101
Name        : Rahul Sharma
Age         : 30
Salary (₹)  : 55000.00
Department  : IT

Employee 2
Employee ID : 102
Name        : Priya Mehta
Age         : 28
Salary (₹)  : 60000.00
Department  : HR

Explanation

Function

Purpose

struct Employee

Defines employee details like ID, name, age, salary, and department.

inputEmployees()

Takes user input for all employee records.

displayEmployees()

Displays the stored employee data in a clear format.

emp[100]

Array of structure to store up to 100 employees.