Union within Structure
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
student
{
int marks;
union
{
char name[20];
int usn;
} u;
} s;
main()
{
int
ch;
printf("\n Enter marks:");
scanf("%d", &s.marks);
printf("\nName or USN
choice:");
printf("\n 1.Name 2.USN:
");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter name:");
scanf("%s",
s.u.name);
printf("\nYour
name is:%s", s.u.name);
break;
case 2: printf("\nEnter usn:");
scanf("%d",
&s.u.usn);
printf("\nYour
usn is:%d", s.u.usn);
break;
}
printf("\nYour marks
is:%d", s.marks);
}
Output:
Enter
marks: 80
Name or USN
choice:
1.Name
2.USN: 1
Enter name:
AAAAA
Your name
is: AAAAA
Your marks
is: 80
No comments:
Post a Comment