include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    char name[30];
    int id;
    double salary;
} Employee;
 
int main()
{
    //number of employees
    int n=2;

    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n",i+1);

        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);

        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);

        //Salary
        printf("Salary: ");
        scanf("%lf",&employees[i].salary);

        //to consume extra '\n' input
        char ch = getchar();
 
        printf("\n");
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int 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("%.2lf \n",employees[i].salary);
 
        printf("\n");
    }
 
    return 0;
}
The output of the Program:

C Program for Employee Details using Structure
Calculate Salary of the Employee from Basic
In the above program, we are directly asking the user to input the salary of every employee.

In case we need to calculate the salary from the basic, we will have to add an extra variable (i.e. basic_salary) to the structure and calculate the net_salary using the formula:  net_salary = basic_salary + HRA + DA – PF

Where,

HRA is 10% of basic salary i.e., HRA=basic_salary*0.1;
DA is 5% of basic salary i.e., DA=basic_salary*0.05;
PF is 12% of basic salary i.e., PF=basic_salary*0.12;
Putting them in the formula: net_salary = basic_salary * (1+ 0.1 + 0.05 – 0.12).

If we modify the above program then the code will be:

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    
    char name[30];
    int id;
    double basic_salary;
    double net_salary;
    
} Employee;
 
int main()
{
    //number of employees
    int n=2;
    
    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n", (i+1));
        
        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);
        
        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);
        
        //Salary
        printf("Basic Salary: ");
        scanf("%lf",&employees[i].basic_salary);
        
        //to consume extra '\n' input
        char ch = getchar();
        
        employees[i].net_salary = employees[i].basic_salary*1.03;
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int 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("%.2lf \n",employees[i].net_salary);
 
        printf("\n");
    }
 
    return 0;
}
If you didn’t understand any part of the code then do comment below.


THIS POST HAS 30 COMMENTS
Balaji FEBRUARY 20, 2020 REPLY
What is the difference between array and structure array

Adarsh Kumar FEBRUARY 21, 2020 REPLY
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 of Employee array[10], we mean to store structure objects into the array.

Balaji FEBRUARY 20, 2020 REPLY
Why you have write the Employee; after defining structure

Adarsh Kumar FEBRUARY 21, 2020 REPLY
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 as Employee. so that we can address the structure in main funtion using just Employee instead of struct Employee.

Shaik Amin APRIL 9, 2020 REPLY
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.?

Adarsh Kumar APRIL 10, 2020 REPLY
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.

  for(i=0; i 10000){
        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");
      }
    }
Mayuresh DECEMBER 15, 2021 REPLY
create a structure employee (eno, ename, salary) Accept details of n employees and write a menu driven program to perform the following Quetions.
1.isplay all employees having salary > 5000
2.Display all employees

Can you solve this?

Rekha APRIL 23, 2020 REPLY
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

Ritik vani APRIL 23, 2020 REPLY
How to add date of joining of the employee in the program

Adarsh Kumar APRIL 25, 2020 REPLY
Add another char array (string) to store the date of joining.

typedef struct{
 
    char name[30];
    int id;
    int salary;
    char dateOfJoining[20];
 
} Employee;
Jayashree APRIL 24, 2020 REPLY
In case of storing 20 employees,we need to change ?

Adarsh Kumar APRIL 25, 2020 REPLY
change the value of n

n=20
Amira osama OCTOBER 30, 2020 REPLY
if I want to print details of one employee by his id what should I do?

Adarsh Kumar OCTOBER 30, 2020 REPLY
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.

int id;
scanf("%d", &id);  
for(int i=0; i<n; i++){
    
    if(employees[i].id == id){
        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);
    }
    
}
Sakshi SEPTEMBER 16, 2021 REPLY
Hi Adarsh, could you please tell me how can I compute total salary outgo for a month . For an employee using structures in C.
Thanks in advance .

Adarsh Kumar SEPTEMBER 17, 2021 REPLY
Updated the post with the solution.

Sakshi SEPTEMBER 17, 2021 REPLY
Thank you so much for the reply Adarsh . This has helped me .

Sakshi SEPTEMBER 20, 2021 REPLY
Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month.

Could you please provide a solution for this pleasee?

Sakshi SEPTEMBER 20, 2021 REPLY
The above program is giving error as ” array subscipt is not an integer” kindly suggest.

Adarsh Kumar SEPTEMBER 24, 2021 REPLY
From my side, the code is working fine. Maybe you are trying to input the full name of the employee. Can you re-check, I have modified the code.

Rohan SEPTEMBER 21, 2021 REPLY
Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month ??

can you please share code for the same .

Muhereza EMMANUEL OCTOBER 27, 2021 REPLY
How can compute the total amont if the employees

hannah NOVEMBER 26, 2021 REPLY
how can I delete a certain employee from the list of array?

Ansh patel MAY 7, 2022 REPLY
Can do you can prepare the code on this
Create a structure to store employee’s data with the following information: Employee’s no., Employee’s name, Employee’s pay, date of joining (which is itself a structure) (i) It is decided to increase the pay as per the following rules: Pay<=Rs. underline 200 : 15 % increase Pay R . 2000: 10% increase Pay > Rs * .5000 : no increase Write a program to do this. (Assume there are 200 employees.) (ii) Write a program to print details of employees who have completed 20 years service.”

Saksham Subedi DECEMBER 1, 2022 REPLY
Write a program to input emp no, name and salary of 100 employees using structure. Display the name and salary of employee using structure.

Sania DECEMBER 9, 2022 REPLY
Plz help me write a code to find the employee whose getting highest salary…

wan aniq JANUARY 7, 2023 REPLY
how we can input the n? means n should entered by users in output

Shweta MAY 4, 2023 REPLY
Hey adarsh can u help me with this
Prepare a menu driven program for employee record system
1.Add record
2.List record

shashank JUNE 30, 2023 REPLY
how can you delete a record

aboyzs JULY 3, 2023 REPLY
Hello, please help on how to create an employee structure to store five employee records in a linked list. The employee structure is given as:

struct employee {
int id;
char name[25];
float salary;
} records [5];
Thanks..

Leave a Reply
Comment
Your comment here...
Enter your name or username to comment
Name (required)
Enter your email address to comment
Email (required)
Enter your website URL (optional)
Website
 Save my name, email, and website in this browser for the next time I comment.
Search
Search
Sharing Is Caring:

Related Topics:
C Program for Student Details using Structure
Solve Tower of Hanoi using Recursion in C
C Program to Sort Array of Strings in Alphabetical Order
C Program for Magic Number
C Program for Perfect Number
Factorial of a Number in C [4 Methods]
About UsContact UsPrivacy PolicySitemap
2023 @pencilprogrammer.com

notification icon 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}