When working with multiple records in C, structures help in organizing complex data. In this post, we’ll define a structure named struct personal
to store details of 5 employees, including their name, date of joining, and salary.
Problem Statement
- Define a structure
struct personal
. - Members should include:
- Name (string)
- Date of Joining (string)
- Salary (float)
- Read data for 5 people.
- Print the details to the screen.
Structure Design in C
Here’s how we can define the structure:
struct personal {
char name[50];
char dateOfJoining[20];
float salary;
};
Full C Program: Read and Display Data of 5 Employees
#include <stdio.h>
#define SIZE 5
// Define the structure
struct personal {
char name[50];
char dateOfJoining[20];
float salary;
};
int main() {
struct personal people[SIZE];
// Input data for 5 people
for (int i = 0; i < SIZE; i++) {
printf("\nEnter details for Person %d:\n", i + 1);
printf("Name: ");
scanf("%s", people[i].name);
printf("Date of Joining (DD-MM-YYYY): ");
scanf("%s", people[i].dateOfJoining);
printf("Salary: ");
scanf("%f", &people[i].salary);
}
// Display data
printf("\n--- Employee Information ---\n");
for (int i = 0; i < SIZE; i++) {
printf("\nPerson %d\n", i + 1);
printf("Name: %s\n", people[i].name);
printf("Date of Joining: %s\n", people[i].dateOfJoining);
printf("Salary: %.2f\n", people[i].salary);
}
return 0;
}
Sample Output
Enter details for Person 1:
Name: Alice
Date of Joining (DD-MM-YYYY): 12-01-2020
Salary: 50000
...
--- Employee Information ---
Person 1
Name: Alice
Date of Joining: 12-01-2020
Salary: 50000.00
Explanation
- We define a structure
personal
to store individual records. - An array
people[5]
is used to handle multiple entries. scanf()
is used to read data from the keyboard.- A loop prints all collected data in a clean format.
Final Thoughts
This example demonstrates how to use structures and arrays in C to manage data effectively. Using struct personal
, you can easily handle employee records or similar datasets in real-world C applications.
Experiment with additional fields like address, ID, or department to extend this program further.