Using structures, write the necessary declaration for Employees in a company.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

typedef struct {

    int employee_id;
    char *first_name[45];
    char *last_name[45];
    char *email[45];
    char *phone_number[9];
    char *hire_date[10];
    int job_id;

} EMPLOYEE;

int main(int argc, char ** argv) {

    EMPLOYEE emp;
    printf("Employee ID: ");  scanf("%d", &emp.employee_id);
    printf("\nFirst Name: ");  scanf("%s", emp.first_name);
    printf("\nLast Name: ");  scanf("%s", emp.last_name);
    printf("\nEmail: ");  scanf("%s", emp.email);
    printf("\nPhone Number: ");  scanf("%s", emp.phone_number);
    printf("\nHire Date: ");  scanf("%s", emp.hire_date);
    printf("\nJob ID: ");  scanf("%d", &emp.job_id);

    printf("+-----------------------------------------------------------------------------------+\n");
    printf("|%11s|%10s|%9s|%18s|%12s|%11s|%6s|\n", "Employee ID", "First Name", "Last Name", "Email", "Phone Number", "Hire Date", "Job ID");
    printf("+-----------------------------------------------------------------------------------+\n");
    printf("|%11d|%10s|%9s|%15s|%12s|%11s|%6d|\n", emp.employee_id, emp.first_name, emp.last_name, emp.email, emp.phone_number, emp.hire_date, emp.job_id);
    printf("+-----------------------------------------------------------------------------------+\n");


    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

 Exercise 4.1 Results

Brief Explanation

  • A structure in C is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
  • Using typedef, EMPLOYEE is created as a synonym for the structure. EMPLOYEE is then use in the main function to define a variable emp of type structure. i.e EMPLOYEE emp
  • scanf function reads data and assign to the fields define in the structure
  • Using printf function, the inputted data is displayed in a nice format to the stdout.  

Add comment


Security code
Refresh