C program to Use structure within union & display the contents of structure elements
Problem Statement :Create one Structure and declare it inside union.Then accept values for structure members and display them.
Write a program to use structure within union , display the contents of structure elements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<stdio.h> #include<conio.h> void main() { struct student { char name[30]; char sex; int rollno; float percentage; }; union details { struct student st; }; union details set; printf("Enter details:"); printf("\nEnter name : "); scanf("%s", set.st.name); printf("\nEnter roll no : "); scanf("%d", &set.st.rollno); flushall(); printf("\nEnter sex : "); scanf("%c", &set.st.sex); printf("\nEnter percentage :"); scanf("%f", &set.st.percentage); printf("\nThe student details are : \n"); printf("\name : %s", set.st.name); printf("\nRollno : %d", set.st.rollno); printf("\nSex : %c", set.st.sex); printf("\nPercentage : %f", set.st.percentage); getch(); } |
Output :
1 2 3 4 5 6 7 8 9 10 11 | Enter details: Enter name : Pritesh Enter rollno: 10 Enter sex: M Enter percentage: 89 The student details are: Name : Pritesh Rollno : 10 Sex : M Percentage : 89.000000 |