Unions in c programming with example

    1 Votes

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:

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Usage of Unions

The main characteristic of union is that it lets you to use same memory location for different data type. In real life C programming, you may encounter a situation where you need to store a value of say 4 byte but at different time according to situation you need to know the value of 1st byte or 3rd byte or may be 4th byte contained value. Yes there is bitwise operators to do that but if you define a union like given above, you can access any byte of value at any time without any logical or arithmetic operation.

int main()
union my_union
char my_char;
int my_int;
float my_float;
} my_union;

my_union.my_char = 'A';
printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float );

my_union.my_int = 57;
printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float );

my_union.my_float = 101.357;
printf("Here is the Output:\n%c\n%i\n%.3f\n", my_union.my_char, my_union.my_int, my_union.my_float );
return 0;

One example to use this is that to know endianness of you machine.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Deference between Structures and Unions

Structure and union both are user defined data types. The main difference between structure and union is that union uses same memory location for all the member variables hence the size of the union is the size of the largest member variable but structure uses different memory location for all the members. Let’s take an example:

union my_union
int my_int;
char my_char1;
char my_char2;
} my_union;

struct my_struct
int my_int;
char my_char1;
char my_char2;
} my_struct;

Now if we execute sizeof function over my_struct and my_union then we will see that the output as 4 for union and 6 for structure. As for structure it is simple 4 for integer and 2 for 2 character variables but if for union it happens because of the union’s property of sharing memory location to all members. Union allocates memory equal to the size of the member variable which takes largest in the memory. Here since integer takes 4 bytes and character takes 1 byte each that’s why it takes 4 bytes of memory hence it outputs 4.

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
C Storage Class Specifiers - auto, register, static and extern