In: Computer Science
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name (size 50) and the salary (double) of an employee. The student_record struct will have the name (size 30), Id number (int) of a student, and the grade (int). Include these two structs in a union called 'person'. Using file redirection called input.txt, at first, ask the user for the employee's information and print them from the main() and the size of the union. Then ask the user for the student's information, then check the grade if it is equal or greater than 90; if yes, print all info, the size of the union, and print "Well done" otherwise print "Keep working" with all info.
The c program developed using Standard input for union and structure for employee and student.
for redirected input file use command :
% a.out < input.txt
#include <stdio.h>
//union inside 2 structure employee_record and student_record
union Person{
struct employee_record
{
char name[50];
double salary;
}e;
struct student_record
{
char name[30];
int Id;
int grade;
}s;
};
// main code
int main()
{
union Person P; // assign P as union variable
printf("Enter the name of Employee: ");
scanf("%s",P.e.name);
printf("Enter the salary of Employee: ");
scanf("%lf",&P.e.salary);
printf( "\nEmployee Name : %s \nEmployee Salary : %lf\n", P.e.name,P.e.salary);
printf( "\nMemory size occupied by data : %lu \n", sizeof(P));
printf("\nEnter the name of Student: ");
scanf("%s",P.s.name);
printf("Enter the ID of student: ");
scanf("%d",&P.s.Id);
printf("Enter the grade of student: ");
scanf("%d",&P.s.grade);
printf( "\nStudent Name : %s \nStudent ID : %d", P.s.name,P.s.Id);
printf( "\nMemory size occupied by data : %lu \n", sizeof(P));
if(P.s.grade >= 90){
printf("\nWell done");
}
else{
printf("\nKeep working");
}
return 0;
}
Output: