C program to store and display n employees info using Union

store and display n employees info using Union

     Union allocates one common storage space for all its members therefore we print the info as soon as we store it. If we print after storing all information, it will print corrupt values. See the output of program given bellow to understand it properly. 


Program:

/*Program to store employee info, By SlashMyCode.com*/
#include <stdio.h>
#include <conio.h>
union employee{
    char name[50];
    int eid;
    float salary;
};
int main(){
    union employee e[10];
    int i,n;

    printf("Enter number of employees ");
    scanf("%d",&n);
    printf("\n");

    for(i=0;i<n;++i)
    {
        printf("Enter information of employee %d:\n",i+1);
        printf("Enter eid: ");
        scanf("%d",&e[i].eid);
        printf("\nInformation for eid number %d: \n",e[i].eid);

        printf("Enter name: ");
        scanf("%s",e[i].name); /* while storing string we don't use '&'. */

     printf("Name: ");
     puts(e[i].name);

        printf("\n");
        printf("Enter salary: ");
        scanf("%f",&e[i].salary);
        printf("salary: %f",e[i].salary);
printf("\n======================================\n");
    }
/*Program to store employee info, By SlashMyCode.com*/
   return 0;

}


Output:
output of union in c






No comments:

Post a Comment

Feel free to ask us any question regarding this post