Program to illustrate student information using union
#include <stdio.h>
union student
{
// defining union member variables
char name[33];
int sno,sub1,sub2;
float tot;
} s;
int main()
{
printf("\n Enter student number :");
scanf("%d",&s.sno);
printf("\n Enter student name :");
scanf("%s",s.name);
printf("\n Enter student mark in subject1 :");
scanf("%d",&s.sub1);
printf("\n Enter student mark in subject2 :");
scanf("%d",&s.sub2);
s.tot = s.sub1 + s.sub2;
printf("\n Total marks = %f ",s.tot);
return 0;
}
output:
#include <stdio.h>
union student
{
// defining union member variables
char name[33];
int sno,sub1,sub2;
float tot;
} s;
int main()
{
printf("\n Enter student number :");
scanf("%d",&s.sno);
printf("\n Enter student name :");
scanf("%s",s.name);
printf("\n Enter student mark in subject1 :");
scanf("%d",&s.sub1);
printf("\n Enter student mark in subject2 :");
scanf("%d",&s.sub2);
s.tot = s.sub1 + s.sub2;
printf("\n Total marks = %f ",s.tot);
return 0;
}
output:
No comments:
Post a Comment