C PROGRAM USING STRUCTURES EMPLOYEE DETAILS
The user enters the employment details into this programme, which then stores them in a specified structure and prints them.
Algorithm:
- Create a single structure to keep an employee's details in your program for employee details. This structure can store an employee's ID, name, age, classification, department, and compensation.
- Define the structure's variables.
- Request that the user enter the details one at a time, then store them in the detail’s variable.
- Print the content or details of employee.
Implementing by using structures for displaying the details and the storing the details of an Employee:
#include <stdio.h>
struct Employee
{
int e_id, e_age, e_sal;
char e_name[35], e_designation[35], e_dept[35];
};
int main()
{
struct Employee e;
printf("Enter id of the Employee: ");
scanf("%d", &e.e_id);
printf("Enter age of the Employee: ");
scanf("%d", &e.e_age);
printf("Enter name of the Employee: ");
getchar();
fgets(e.e_name, 35, stdin);
printf("Enter designation of Employee: ");
fgets(e.e_designation, 35, stdin);
printf("Enter department of the Employee: ");
fgets(e.e_dept, 35, stdin);
printf("Enter salary of the Employee: ");
scanf("%d", &e.e_sal);
printf("\nDetails of an Employee:\n");
printf("Id of an Employee: %d\n", e.e_id);
printf("Name of an Employee: %s", e.e_name);
printf("Age of an Employee: %d\n", e.e_age);
printf("Designation of an Employee: %s", e.e_designation);
printf("Department of an Employee: %s", e.e_dept);
printf("Salary of an Employee: %d\n", e.e_sal);
return 0;
}
OUTPUT:
![C PROGRAM USING STRUCTURES EMPLOYEE DETAILS](https://static.tutorialandexample.com/c/c-program-using-structures-employee-details1.png)
![C PROGRAM USING STRUCTURES EMPLOYEE DETAILS](https://static.tutorialandexample.com/c/c-program-using-structures-employee-details2.png)
Explanation:
Here,
- We have defined a structure called "Employee" to hold employee information. The ID, age, salary, name, designation, and department values can all be stored in this structure.
- We are establishing the Employee variable e inside main. We'll read the information and put it in this variable's fields.
- We are reading the data and putting it in the fields of the variable e using printf, scanf, and fgets.
- To access a property inside a structure, type dot(.) is used.
- After the reading is complete, we publish the material, which is the employee's information.
Implementing by using the structures and methods for displaying the details and the storing the details of an Employee:
#include <stdio.h>
struct Employee
{
int e_id, e_age, e_sal;
char e_name[35], e_designation[35], e_dept[35];
};
struct Employee Read_Employee_Details(struct Employee e)
{
printf("Enter id of the Employee: ");
scanf("%d", &e.e_id);
=printf("Enter age of the Employee: ");
scanf("%d", &e.e_age);
printf("Enter name of the Employee: ");
getchar();
fgets(e.e_name, 35, stdin);
printf("Enter designation of the Employee: ");
fgets(e.e_designation, 35, stdin);
printf("Enter department of the Employee: ");
fgets(e.e_dept, 35, stdin);
printf("Enter salary of the Employee: ");
scanf("%d", &e.e_sal);
return e;
}
void Print_Employee_Details(struct Employee e)
{
printf("\nDetails of an Employee:\n");
printf("Id of an Employee: %d\n", e.e_id);
printf("Name of an Employee: %s", e.e_name);
printf("Age of an Employee: %d\n", e.e_age);
printf("Designation of an Employee: %s", e.e_designation);
printf("Department of an Employee: %s", e.e_dept);
printf("Salary of an Employee: %d\n", e.e_sal);
}
int main()
{
struct Employee e;
e = Read_Employee_Details(e);
Print_Employee_Details(e);
return 0;
}
OUTPUT:
![C PROGRAM USING STRUCTURES EMPLOYEE DETAILS](https://static.tutorialandexample.com/c/c-program-using-structures-employee-details3.png)
![C PROGRAM USING STRUCTURES EMPLOYEE DETAILS](https://static.tutorialandexample.com/c/c-program-using-structures-employee-details4.png)
Explanation:
- Here, we have developed two novel techniques. Both reading and printing the data are options.
- One structure of the type Employee is required as a parameter for Read_Employee_Details. The structure variables are given these values after the data from the user is read. The identical structure is returned.
- In the main method, we are assigning e the result of Read_Employee_Details.
- One structure is given to the Print_Employee_Details method, which prints its contents. The type of this structure ought to be Employee.
- Only these two methods are being called inside of main, and they will produce results that are comparable to those in the prior example.