Use structure within union - C Language Programming

Use structure within union - C Language Programming

Program in C Language to use structure within union



Click here to open this program in Turbo C++

/**********************************************************
 Statement - Use structure within union
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************************/


#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 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();
}
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.