union in c programming
C programming Tutorials

Union in C Programming

Union in C Programming

Union in C Programming is an important concept that allow  different data types to be stored in same location.

Union in C Programming is an important topic for computer science students. Some time questions based on Union are asked in GATE(CS) and UGC NET exam.

Today in this Union in C tutorial we will learn about some basics concepts of Union.

Subtopics of Union

  • Introduction of Union.
  • Declaration and definition of Union.
  • Size of Union.
  • Program for Union Concepts.
  • Example of Union Concepts.
  • Program to Access the Union Members

Frequently Asked Questions in Union

After reading this union in c tutorial students will be able to answer the following questions

  • What is Union ?
  • What is difference between Union and structure ?
  • How we declare and define the Union?
  • How can we determine the size of determine ?
  • Write a program to implement the Concept of Union in C.
  • How the members of Union can be access ?

Let’s start with introduction of Union in C

What is Union in c programming?

  • Union in C is also a user defined data type.
  • The only difference of union in C with structures is unlike structures, unions share the same memory location.

Let us consider we have defined a structure abc and a union abc, as

union in c

  • As the union members share the same location, the change made in one member will be reflected to other member as well.
  • We use keyword union to define union.
  • The syntax of union in c programming is very similar to that of Structure in C.

Union in C Examples

A simple program for union in c examples is given below –

Example 1: Program to Declare and Define Union

union abc

{

int a; //an integer

char b; //a character

} var ;

int main()

{

var.a=65;

printf(“a=%d\n”,var.a);

printf(“b=%c”,var.b);

return 0;

}

Output

a=65

b=A

Union keyword is used to declare or define the Union in C Programming.

In the above program abc is Union .In the above program we get a=55 and b=A. We know that in union, both union members of union share the same memory location.

When we define int a=65, 65 is also shared by character b. Again, the ASCII code of A is 65.

Hence when b is printed as a character it prints A. But if we print b as an integer, it will print b=65 too.

Size of the Union

Size of the union in c is taken according to the size of the largest member of the union.

We can understand the concept of size of Union by the help of following program

Example 2: Program to Calculate the Size of Union

#include <stdio.h>

union abc{

int a;

char b;

double d;

float f;

}var;

int main(){

printf(“size of int a=%ld\n”,sizeof(var.a));

printf(“size of char b=%ld\n”,sizeof(var.b));

printf(“size of double d=%ld\n”,sizeof(var.d));

printf(“size of float f=%ld\n”,sizeof(var.f));

printf(“size of union abc=%ld”,sizeof(union abc));

return 0;

}

Output

size of int a=4

size of char b=1

size of double d=8

size of float f=4

size of union abc=8

Here the size of union=8=size of double (largest member of the union).

Accessing members of the union using pointers:

We can access members of the union using a (->) operator.

Union Program in for Accessing the Union Member

Example 3: Program for accessing the union members 

#include <stdio.h>

union abc{

int a;

char b;

}var;

int main(){

union abc var;

var.a=55;

union abc *p=&var; //using pointer p

printf(“a=%d\nb=%c”,p->a,p->b);

return 0;

}

Output

a=55

b=7

Note – Unions are used for better memory management in programs.

From all the members of the union, the user can use only one member at a time. The memory of the largest member of the union will be allocated to all the members of the union.

When all the members share the same memory location, any one of the members may be active.

We can understand it by with a simple program given below –

#include <stdio.h>

union abc{

int a;

char b;

float f;

}var;

int main(){

var.a=78;

var.b=”Code”;

var.f=9.87;

printf(“a=%d\n”,var.a);

printf(“b=%c\n”,var.b);

printf(“f=%f”,var.f);

return 0;

}

Output

a=1092479877

b=�

The value of a and b are corrupted as all the memory is allocated to all the members of the union. Only value of float f is printed correctly.

Difference  between Structure and Union

Both a structure and a union combine different information related to a given entity. The main difference between union and structure is how specific information is stored and accessed.

some differences between union and structure are given below-

  • We use the struct keyword to define a structure whereas we use the union keyword to define a union.
  • In structure every member is assigned a unique memory location where as i Union all the data members share a memory location.
  • Change in the value of one data member does not affect other data members in the structure where as Change in the value of one data member affects the value of other data members.
  • I structure we can initialize multiple members at a time whereas in union we can initialize only the first member at once.
  • Size of a structure is the sum of the size of every data member where as A union’s total size is the size of the largest data member.
  • Users can access or retrieve any member at a time. You can access or retrieve only one member at a time.

Conclusion and Summary

We have learned bout the several concepts of Union in C Programming in this tutorial. I hope this tutorial will be helpful for students.

Students are requested to provide their feedback on this tutorial so that we can improve the quality of our tutorial.

If you have any query regarding this Tutorial then feel free to ask in comment section.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *