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*/

union emp
char name[20];
int id;
float sal;

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

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

Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-