About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Wednesday, September 4, 2013

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

#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

No comments: