I had the following C program
#include <stdio.h>
union student{
char name[20];
int id;
float percentage;
}student1;
int main()
{
printf(" Enter name, id and then percentage \n");
scanf("%s",student1.name);
scanf("%d",&student1.id);
scanf("%f",&student1.percentage);
printf("Details of student are:");
printf("Name %s, ID: %d, Percentage: %f", student1.name, student1.id, student1.percentage);
}
The output is as follows:
Enter name, id and then percentage
Nik
90
7.9
Details of student are:Name ═╠ⁿ@, ID: 1090309325, Percentage: 7.900000
Process returned 0 (0x0) execution time : 7.665 s
Press any key to continue.
The question is why this doesn't work for unions.