Union

The concept of the union is same like the structure the difference is of their storage method. All the data-members of the union are share same storage area. Union is very useful where we don’t want to assign the values to all the data members at same time.

e.g:     

union employee

{

int id;

char name[20];

float salary;

}emp;

And access of the union data-members is same like the structure.

e.g:      emp.id;

Program:

PROGRAM TO READ INFORMATION OF A PERSON FROM KEYBOARD AND PRINT THE SAME ON SCREEN USING STRUCTURE AND UNION

#include<stdio.h>

#include<conio.h>

void main()

{

union doj

{

char date[10];

};

struct person

{

char name[15];

int salary;

union doj d1;

}p1;

clrscr();

printf(“Enter name of the person: “);

gets(p1.name);

printf(“Enter date of joining: “);

gets(p1.d1.date);

printf(“Enter salary of the person: “);

scanf(“%d”,&p1.salary);

printf(“\nThe given information of the person is:\n”);

printf(“Name of person is: %s\n”,p1.name);

printf(“Date of joining is: %s\n”,p1.d1.date);

printf(“Salary of the person is: %d”,p1.salary);

getch();

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *