Header Ads Widget

Union in C

 Union :-
                Union is predefine data type 
  1. Capable of grouping different types data items in a single unit.
  2. The union variable is allocated a common memory equal to the memory size required by the maximum sized data member.
  3. All members share the same common memory but one at a time .
  4. Union are used to efficiently use the memory.


if int = 2 byte , char = 1 bytes , float =4 bytes for the 16 bits architecture

/* WAP to  read name, id and display by using union data type*/

#include<stdio.h>
union emp
{
char name[20];
int id;
float sal;
}e1;

int main()
{
printf("Enter id of employee\n");
scanf("%d",&e1.id);
printf("ID - %d\n",e1.id);
printf("Enter name of employee\n");
scanf("%s",e1.name);
printf("Name - %s\n",e1.name);
printf("Enter salary of employee\n");
scanf("%f",&e1.sal);
printf("Salary - %f\n",e1.sal);
return 0;
}

Output:-
Enter id of employee
12568
ID - 12568
Enter name of employee
Rajnish
Name - Rajnish
Enter salary of employee
85568.23
Salary - 85568.226562




Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-