Unions in C: Syntax, Uses, advantages

A union is a memory location shared by two or more different variables.

Syntax of a union:

union union_type_name {

type element_name;

type element_name;

type element_name;



} variables;


union u {

int a;

char b;

float c;


Uses and advantages of Unions:

1) Unions are used for generating portable codes which are machine independent.

2) While declaring unions you don’t have to worry about sizes of int, long, float or any other data types.

3) It is usually used for specialized type conversions.

Leave a Reply