Union

  • A union is a special data type available in C that enables you to store different data types in the same memory location.
  • You can define a union with many members, but only one member can contain a value at any given time.
  • Unions provide an efficient way of using the same memory location for multi-purpose.

Example:

typedef struct Demo
{
int n; //4
double d; //8
char str[20]; //24
} DEMO;
int main( )
{
DEMO d;
d.n= 100000;
d.d= 123456780.01;
strcpy(d.str,”This is demo”);
printf(“%d\n”,d.n);
printf(“%lf\n”,d.d);
printf(“%s\n”,d.str);
}

Classical usage of UNION:

null

Mr. Sandeep Soni

Founder, Trainer & CEO, Deccansoft Software Services.

Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.