BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to read and print employee details using structure

Last Updated: May 14, 2024 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, we will learn how to write a C program to read and print employee details using structures.

C Program for employee details using structure

#include <stdio.h>

// Define the structure for employee details
struct Employee {
int id; // Employee ID
char name[50]; // Employee Name
float salary; // Employee Salary
};

int main() {
// Declare a variable of type struct Employee
struct Employee emp;

// Prompt the user to enter the employee's ID and store it in emp.id
printf("Enter Employee ID: ");
scanf("%d", &emp.id);

// Prompt the user to enter the employee's name and store it in emp.name
// Note: %s reads a string until a space or newline is encountered
printf("Enter Employee Name: ");
scanf("%s", emp.name);

// Prompt the user to enter the employee's salary and store it in emp.salary
printf("Enter Employee Salary: ");
scanf("%f", &emp.salary);

// Display the employee's details
printf("\nEmployee Details:\n");

// Print the employee's ID
printf("ID: %d\n", emp.id);

// Print the employee's name
printf("Name: %s\n", emp.name);

// Print the employee's salary
printf("Salary: %.2f\n", emp.salary);

// Indicate that the program ended successfully
return 0;
}

How this program works?

  1. Include Header File:
    #include <stdio.h>
    This line includes the standard input-output library in our C program, which is required for using printf() and scanf() functions.
  2. Structure Definition:
    struct Employee {
    int id;
    char name[50];
    float salary;
    };
    This defines a structure named Employee which contains three fields: an integer id, a character array name (to hold the employee’s name), and a float salary. This is to store the employee details so that we can later fetch the details from it and print it.
  3. Main Function:
    int main() {
    struct Employee emp;
    This declares a variable emp of type struct Employee. This variable emp refers to the structure so we can use it to access structure for reading and writing data into it.
  4. Taking Input:
    printf("Enter Employee ID: ");
    scanf("%d", &emp.id);

    printf("Enter Employee Name: ");
    scanf("%s", emp.name);

    printf("Enter Employee Salary: ");
    scanf("%f", &emp.salary);
    Here printf statements are prompts to the users asking for input and scanf statements store the user input into the structure using emp.
  5. Displaying Details:
    printf("\nEmployee Details:\n");
    printf("ID: %d\n", emp.id);
    printf("Name: %s\n", emp.name);
    printf("Salary: %.2f\n", emp.salary);
    These lines print the employee’s details stored in the structure using emp variable.

Top Related Articles:

  1. C Program to Print Pyramid Star Pattern
  2. C Program to Add two distances using structure
  3. C Program for employee salary calculation using structure
  4. C Program to Read the First Line From a File
  5. C Program to convert a time from 12 hour to 24 hour format

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap