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