Problem: Write a C program to read and print employee details using structure.
To store multiple employee details we will use an array of structures. Each element in the array will represent a single employee.
Each Structure i.e. Employee contains:
- Name
- Id
- Salary
In our program, we will read the inputs for each employee from the user, and then output all employee details by iterating through the array using the ‘for’ loop.
C Program to Read and Print Employee Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h> #include <stdlib.h> typedef struct{ char name[30]; int id; int salary; } Employee; int main() { int i, n=2; Employee employees[n]; //Taking each employee detail as input printf("Enter %d Employee Details \n \n",n); for(i=0; i<n; i++){ printf("Employee %d:- \n",i+1); //Name printf("Name: "); scanf("%s",employees[i].name); //ID printf("Id: "); scanf("%d",&employees[i].id); //Salary printf("Salary: "); scanf("%d",&employees[i].salary); printf("\n"); } //Displaying Employee details printf("-------------- All Employees Details ---------------\n"); for(i=0; i<n; i++){ printf("Name \t: "); printf("%s \n",employees[i].name); printf("Id \t: "); printf("%d \n",employees[i].id); printf("Salary \t: "); printf("%d \n",employees[i].salary); printf("\n"); } return 0; } |
Output of Program

If you didn’t understand any part of the code then do comment below.
What is the difference between array and structure array
They both are similar. The difference is the type of data in the array. When we declare
int array[10]
, we mean to store integers into the array and in case ofEmployee array[10]
, we mean to store structure objects into the array.Why you have write the Employee; after defining structure
Any name you put at the end of the structure declaration along with
typedef
at the beginning is used to rename the structure. In our case, we have renamed the structure asEmployee
. so that we can address the structure in main funtion using justEmployee
instead ofstruct Employee
.Write a program to read the details of āNā employees (ID, Name, Salary) and display the details of those employees whose salary is above Rs.10,000/-.
Can any one write this program.?
The logic of your problem can be easily solved using the above program. Just include
if(employees[i].salary> 10000)
indide the loop which prints the employee details.1. Write a Menu Driven C program to create an employee database using structure and perform following operations: (I) Accept Employee Record (II) Display All Employees Records (III) Search Employee Record by name and Display (IV) Search Employee Record by Employee ID and Display (V) Update Employee Record (Note: Employee record will have following fields Employee ID, Employee Name, Employee Department, Employee Salary)
can i pls get a code for this
How to add date of joining of the employee in the program
Add another char array (string) to store the date of joining.
In case of storing 20 employees,we need to change ?
change the value of n
if I want to print details of one employee by his id what should I do?
If you want to print an employee by his id use any of the search algorithms such as linear search or binary search to search an employee in the employee array. If found then output his details.