union – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 05:41:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg union – Programmerbay https://programmerbay.com 32 32 Difference between Union and Structure in C in Tabular Form https://programmerbay.com/difference-between-union-and-structure-in-c/ Wed, 22 Jun 2022 18:52:08 +0000 https://programmerbay.com/?p=5545 Structure and Union are user defined data types, capable of holding data of various types.

The basic difference between structure and union is that structure provides a separate memory location for each member, whereas, Union provides a single memory location that is shared by all of its members.

Difference between Union and Structure in C in Tabular Form

StructureUnion
It can handle different elements of different datatypes at a timeIt can handle only single element at a time of any datatypes
Struct keyword is used to create a variable of a structure typeUnion keyword is used to define variable of a union type
Each element of structure gets memory spaces separatelyAll elements of union have to share the same memory space
The value of an element doesn't get altered when changes are made to other elementsThe value of an element does get altered when changes are made to elements
The size of a structure variable is equivalent to or greater than the sum of its elementsThe size of the union variable is always equivalent to element with the largest datatype
In some situations, it can lead to memory wastage In some situations, it makes a proper memory utilization as it locates memory to the maximum size of the element

Structure

In C, Structures can be defined as a collection of variables of different data types that can be accessed by a single name. Therefore, these elements are together considered as a single entity.

A structure is a user defined type that groups variables of different datatype together in single unit which can be accessed with single name and allows in organizing complex data set in form of records. For example, a record of books in library where a book as structure and its relevant attribute can be title, auther, published date and more as members.
struct keyword is used to define a structure that can consist one or more data members.

The main advantage of the structure is that it makes easier to handle a large set of data.

In terms of memory allocation, a compiler provides memory to each element and the total size of the structure variable is, therefore, equal to or greater than the sum of all members.

It is possible the size can be more than the sum of its member as machine architecture can be varied.

Structure

Points to remember about Structure:

  • They are used to allocate sequentially
  • Compiler allocates memory to every element of the structure separately
  • The size of a structure variable is greater than or equal to the sum of its all elements
  • The object of structure can handle all members at a time

Syntax:

structure structure_name{
Variable,
Variable2
}s;

Code example of structure in C:

#include <stdio.h> 
struct struct_name { 
int variable1; 
int variable2 
};
int main( ) {
struct struct_name str;
str.variable1 = 10;
printf( "Variable1 = %d \n",str.variable1);
printf( "Memory size of structure: %d\n", sizeof(str));
return 0;
}

Output:

Variable1 = 10
Memory size of structure : 8

Union

Union is capable of holding only an individual element’s value at a time. A compiler allocates only a single storage area that can be used to store a value of a specific type.

Union is a user defined data type in which its members share the same memory location, unlike, Structures where each member gets a different memory location. Further, if a member data is changed, then it would also be replaced for other members as well.
Union allocated size is decided accordingly based on the largest data type member in Union.

All elements use the same memory area in order to store value regardless of its type.

Points to remember about Union:

  • A union can handle only a single member at a time
  • The size of the union variable is always equivalent to the member with the largest datatype
  • A compiler allocates the memory by considering the largest data element defined in the union

union

Syntax:

union union_name{
Variable,
Variable2
}u ;

Code example of Union in C :

#include <stdio.h>
 union union_name {
 int variable1;
 int variable2
 };
int main( ) {
union union_name un;
un.variable1 = 10;
printf( "Variable1 = %d \n",un.variable1);
printf( "Memory size of union : %d\n", sizeof(un));
return 0;
}

 

Output:

Variable1 = 10
Memory size of union : 4

 

C program to demonstrate the allocation of memory in case of Structure and Union

In case of Structure :

#include <stdio.h>

struct struct_name
{
  int variable1;
int variable2};

int main ()
{
  struct struct_name str;
  str.variable1 = 10;
  str.variable2 = 20;

  printf ("%p\n", &str.variable1);
  printf ("%p\n", &str.variable2);
  return 0;
}

Output:

0x7ffccf6e9260
0x7ffccf6e9264

In Case of Union:

#include <stdio.h>

union union_name
{
  int variable1;
int variable2};

int main ()
{
  union union_name str;
  str.variable1 = 10;
  str.variable2 = 20;

  printf ("%p\n", &str.variable1);
  printf ("%p\n", &str.variable2);
  return 0;
}

Output: 

0x7fff08cacd44
0x7fff08cacd44

 

 

]]>