Tuesday, March 29, 2016

Unions in c programming with example

Union is another user defined data type in C. This is very similar to the structure but there is slight difference between them.
union my_union
{
int var1;
char ch1;
char ch2;
char ch3;
char ch3;
} union1;

union1.var1 = 220;
union1.ch1 = ‘A’;
printf(“value %d”,union1.var1);

Unions in C

To define union, you have to use keyword union. Other things in syntax for union are similar to the structure. Example of declaration and access of union is given below:
union my_union
{
char my_char;
int my_int;
float my_float;
} my_union;

No comments: