C Language | Structure and Union: Example of structure and union

Example of structure and union

#include<stdio.h>
#include<conio.h>
union student2
{
    int roll;
    char name[20];
    float age;
}s2;
int main()
{
    //clrscr();
    printf("Enter roll ");
    scanf("%d",&s2.roll);
    printf("Enter name ");
    scanf("%s",s2.name);
    printf("Enter age ");
    scanf("%f",&s2.age);
    printf("Roll number %d\n",s2.roll);
    printf("Name %s\n",s2.name);
    printf("Age %f\n",s2.age);
    getch();
    return(0);
}
/* Output */
Enter roll 101
Enter name aaa
Enter age 15
Roll number 
Name
Age 15.000000
#include<stdio.h>
#include<conio.h>
union student2
{
int roll;
char name[20];
float age;
}s2;
void main()
{
clrscr();
printf("Enter roll ");
scanf("%d",&s2.roll);
printf("Roll number %d\n",s2.roll);
printf("Enter name ");
scanf("%s",s2.name);
printf("Name %s\n",s2.name);
printf("Enter age ");
scanf("%f",&s2.age);
printf("Age %f\n",s2.age);
getch();
}
/* Output */
Enter roll 101
Roll number 101
Enter name Amit
Name Amit
Enter age 15
Age 15.000000

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects