Structures in C provide a way to group related data types together. This is particularly useful for storing and managing complex data such as employee information. In this article, we will explore C Program to Store Information of an Employee Using Structure with three different examples.
Prerequisites
Before diving into the solutions, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with structures in C.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Basic Employee Information Storage
1.1 Explanation
In this example, we define a structure to store basic employee information such as name, age, and salary. We then create an instance of this structure, input the data, and display it.
1.2 Program
#include <stdio.h>
// Define a structure to store employee information
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp;
// Input employee details
printf("Enter name: ");
fgets(emp.name, sizeof(emp.name), stdin);
printf("Enter age: ");
scanf("%d", &emp.age);
printf("Enter salary: ");
scanf("%f", &emp.salary);
// Display employee details
printf("\nEmployee Details:\n");
printf("Name: %s", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
1.3 Output
Enter name: John Doe
Enter age: 30
Enter salary: 55000
Employee Details:
Name: John Doe
Age: 30
Salary: 55000.00
Solution 2: Array of Structures
2.1 Explanation
This example demonstrates how to handle multiple employees by using an array of structures. We define an array of Employee
structures, input data for multiple employees, and display it.
2.2 Program
#include <stdio.h>
// Define a structure to store employee information
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp[3];
// Input employee details for multiple employees
for (int i = 0; i < 3; i++) {
printf("Enter details for employee %d\n", i+1);
printf("Enter name: ");
getchar(); // To consume the newline character left by previous input
fgets(emp[i].name, sizeof(emp[i].name), stdin);
printf("Enter age: ");
scanf("%d", &emp[i].age);
printf("Enter salary: ");
scanf("%f", &emp[i].salary);
}
// Display employee details
printf("\nEmployee Details:\n");
for (int i = 0; i < 3; i++) {
printf("Employee %d:\n", i+1);
printf("Name: %s", emp[i].name);
printf("Age: %d\n", emp[i].age);
printf("Salary: %.2f\n", emp[i].salary);
}
return 0;
}
Output
Enter details for employee 1
Enter name: Alice
Enter age: 25
Enter salary: 60000
Enter details for employee 2
Enter name: Bob
Enter age: 28
Enter salary: 65000
Enter details for employee 3
Enter name: Charlie
Enter age: 22
Enter salary: 55000
Employee Details:
Employee 1:
Name: Alice
Age: 25
Salary: 60000.00
Employee 2:
Name: Bob
Age: 28
Salary: 65000.00
Employee 3:
Name: Charlie
Age: 22
Salary: 55000.00
Solution 3: Function to Input and Display Employee Information
3.1 Explanation
This example modularizes the program by using functions to input and display employee information. We define functions to read and print employee details, enhancing code reusability and readability.
3.2 Program
#include <stdio.h>
// Define a structure to store employee information
struct Employee {
char name[50];
int age;
float salary;
};
// Function to input employee details
void inputEmployee(struct Employee *emp) {
printf("Enter name: ");
getchar(); // To consume the newline character left by previous input
fgets(emp->name, sizeof(emp->name), stdin);
printf("Enter age: ");
scanf("%d", &emp->age);
printf("Enter salary: ");
scanf("%f", &emp->salary);
}
// Function to display employee details
void displayEmployee(struct Employee emp) {
printf("Name: %s", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
}
int main() {
struct Employee emp;
// Input employee details
inputEmployee(&emp);
// Display employee details
printf("\nEmployee Details:\n");
displayEmployee(emp);
return 0;
}
Output
Enter name: David
Enter age: 35
Enter salary: 75000
Employee Details:
Name: David
Age: 35
Salary: 75000.00
Conclusion
Using C Program to Store Information of an Employee Using Structure to store and manage complex data such as employee information is a powerful feature. This article presented three different methods to handle employee information: a basic example with a single employee, an array of structures for multiple employees, and a modular approach using functions. Each method serves different needs and can be chosen based on the complexity and requirements of your application. Understanding these techniques will enhance your ability to manage structured data in C programming.