Problem in Union Array program
I created my first union program and now I am not able to complete it. It shows no error.
In outpur window, I cant see the name, there is random symbol at place of name.
HRA and total is showing wrong.
Any hint?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
union salary
{
char name[20];
int age, bas, da, hra, tot;
} s[2];
int i;
for(i=0;i<2;i++)
{
printf("Enter Name");
scanf("%s",s[i].name);
printf("\n Enter Age");
scanf("%d",&s[i].age);
printf("\n Enter Basic Salary");
scanf("%d",&s[i].bas);
}
printf("\n \t \t \t \t ABC LTD");
printf("\n \t \t \t \t Jaipur");
printf("\n -------------------------");
printf("\n Salary Sheet");
printf("\n -------------------------");
printf("\n S. no \t Name \t Age \t Basic \t DA \t HRA \t Total");
int a;
for(a=0;a<2;a++)
{
printf("\n %d",a+1);
printf("\t %s",s[a].name);
printf("\t %d",s[a].age);
printf("\t %d",s[a].bas);
s[a].da=s[a].bas*50/100;
printf("\t %d",s[a].da);
s[a].hra=s[a].bas*10/100;
printf("\t %d",s[a].hra);
s[a].tot=s[a].bas+s[a].da+s[a].hra;
printf("\t %d",s[a].tot);
}
printf("\n ------------------------");
return 0;
}