C program to use structure within union & display the contents of structure elements

January 21, 2025 No Comments » Hits : 298





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

#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;
clrscr();
printf("Enter details:");
printf("\nEnter name : ");
scanf("%s", set.st.name);
printf("\nEnter rollno: ");
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 :

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

Incoming search terms: