Published on: 12th Nov 2010    |   Last Updated on: 24th Nov 2011

Union :

Union is user defined data type used to stored data under unique variable name at single memory location.

Union is similar to that of stucture. Syntax of union is similar to stucture. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time.

To declare union data type, 'union' keyword is used.

Union holds value for one data type which requires larger storage among their members.


	union union_name
		<data-type> element 1;
		<data-type> element 2;
		<data-type> element 3;


	union techno
		int comp_id;
		char nm;
		float sal;

In above example, it declares tch variable of type union. The union contains three members as data type of int, char, float. We can use only one of them at a time.

* Memory Allocation :

	    C union
	 Fig : Memory allocation for union

To access union members, we can use the following syntax.


Program :

/*  Program to demonstrate union.
Creation Date : 10 Nov 2010 09:24:09 PM
Author [Technowell, Sangli] */

#include <stdio.h> #include <conio.h> union techno { int id; char nm[50]; }tch; void main() { clrscr(); printf("\n\t Enter developer id : "); scanf("%d", &; printf("\n\n\t Enter developer name : "); scanf("%s", tch.nm); printf("\n\n Developer ID : %d",;//Garbage printf("\n\n Developed By : %s", tch.nm); getch(); }

Output :

	Enter developer id : 101
	Enter developer name : technowell
Developer ID : 25972

Developed By : technowell_

Link this post on your Blog/Website :

Follow us on :   Technoexam on facebook Technoexam on twitter Technoexam on linkedin Technoexam on youtube