Unions in C : Understanding Memory Allocation


In C, a union is a type of data structure that allows you to store different data types in the same memory location. A union is similar to a structure, but it only allows one member to be active at a time. The size of a union is the size of its largest member.

   Syntax :
        union union_name
        {
           datatype member_1 ;
           datatype member_2 ;
           datatype member_n ;
        }

This program demonstrates the use of union in C programming language.

  • The program defines two data structures, one is a struct called "demo" and the other is a union called "udemo". Both of them have the same two members, one is an integer "a" and the other is a character "b".
  • In the main function, an instance of the union "udemo" is created and named as "o". The program assigns a value 65 to the member "a" of union "o".
  • The program then uses the printf function to display the value of member "a" and "b" of the union "o".
  • It's worth noting that the size of the union "udemo" is equal to the size of its largest member, which is 4 bytes for an integer. On the other hand, the size of the struct "dememo" is equal to the sum of its members' size, which is 4 bytes for an integer and 1 byte for a character, a total of 5 bytes.
  • You can also see the commented lines inside the main function, those lines demonstrate how to find the size of different data types in C and the size of the union and struct defined in the program.
  • It's also important to note that when you assign a value to one member of the union, the previous values of the other members are overwritten and may not be accessible.
  • In this program, when you assign 65 to the member a of union, it will be stored in the memory location as an integer and when you try to access it using the member b, it will be interpreted as a character, which is the ASCII value of 65 which is 'A'.

This program is a simple example of how to use union in C and how to access the members of a union, it also demonstrates the difference in memory usage between union and struct.

Source Code

//Union in C Programming
#include<stdio.h>
 
/*
    1.User Define Data Type
    2.Union Members Share the Same Memory Location
    3.Union Size is based in biggest size of a data type
 
*/
 
struct demo
{
    int a;//4
    char b;
};
union udemo
{
    int a;//4
    char b;
}o;
 
int main()
{
   /* printf("\nInteger : %d",sizeof(int));
    printf("\nchar : %d",sizeof(char));
    printf("\nUnion Size : %d",sizeof(union udemo));
    printf("\nStruct Size : %d",sizeof(struct demo));*/
    o.a=65;
    printf("\nUnion A : %d",o.a);
    printf("\nUnion B : %c",o.b);
    return 0;
}
 
To download raw file Click Here

Output

Union A : 65
Union B : A

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C