Friday 23 September 2011

Union

Union is a collection of dessimller data types elements.
we can creat a union using union key word.
syntax:
             union record 
             { 
              char n[15];
               int age;
               char c[10];
              }e;


Question 1:Write a program to enter the name,age,and city of a employ and print them?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
union record
{
char n[20];
int age;
char c[30];
}e;
clrscr();
printf("enter the name");
gets(e.n);    // scanf("%s",&e.n);
printf("enter the age");
scanf("%d",&e.age);
printf("\nenter the city");
scanf("%s",&e.c);
printf("\n name=%s",e.n);
printf("\n age=%d",e.age);
printf("\ncity=%s",e.c);
getch();
}
Question 2:write a progarm to enter the name,age and city up two student and print them?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
union record 
{
char n[15];
int age:
char c[15];
}
e1,e2;
printf("enter the name");
scanf("%s",&e1.n);
printf("enter the age");
scanf("%d",&e1.age);
printf("enter the city");
scanf("%s",&e1.c);
printf("enter the name of 2nd student");
scanf("%s",&e2.n);
printf("enter the age of second student");
scanf("%d",&e2.age);
printf("enter the city of second student");
scanf("%s",&e2.c);
printf("\n anme=%s",e1.n);
printf("\nage=%d",e1.age);
printf("\ncity=%s",e1.c);
printf("\nname=%s",e2.n);
printf("\nage=%d",e2.age);
printf("\ncity=%s",e2.c);
getch();
}











No comments:

Post a Comment