| by Arround The Web | No comments

Difference Between Structure and Union in C

The C programming language provides programmers with various data types to store data in memory for processing. Among these data types, structures, and unions are two commonly used datatypes in C programming to group multiple variables of different data types into a single unit. Both structures and unions are defined by the programmer and serve similar purposes. However, they differ in several aspects, including storage, memory allocation, and usage.

The article provides a comparative analysis between structure and union in the C programming language.

Structure in C

A structure in C is a user-defined datatype that allows programmers to group variables of different data types into a single entity. A structure can contain members, which can be variables of any data type, such as integers, floats, arrays, or even other structures. To declare a structure in C, you must use the keyword “struct” followed by the name of the structure and its member variables, as shown in the following example:

Declaration of a Structure in C

The following is the declaration of structure in C programming.

struct_keyword struct_name{

data_type1 member1;

data_type2 member2;

};

Union in C

A union in C is also a user-defined datatype that allows the programmer to store different types of data in the same memory location. Unlike structures, unions have only one memory location that is shared by all their members. Therefore, only one union member can access it at one time. A union’s size is measured by the size of its biggest member.

Declaration of a Union in C

Here is an example of how to declare a union in C.

union_keyword union_name {

  data_type1 member1;

  data_type2 member2;

};

Difference Between Structure and Union in C

The following table shows the difference between structure and union in C programming.

Feature Structure Union
Definition Collection of related data members The same memory location is shared by all members
Memory Allocation Allocates memory for all members Allocates memory for the largest member
Initializing Members Can be initialized individually or as a whole Can only initialize one member at a time
Default Initialization Members are initialized to default values Members are uninitialized by default
Size Calculation The Sum of the sizes of all members Size of the largest member
Use Cases Used to store multiple types of data and access them together Used to save memory when multiple data types share the same memory location
Accessing Members Using member name and dot (.) operator Using member name and dot (.) operator

The following is a coding example that illustrates the difference between structure and union in C programming.

#include <stdio.h>

struct MyStruct {

  int a;

  char b;

};

union MyUnion {

  int x;

  char y;

};

int main() {

  struct MyStruct myStruct;

myStruct.a = 10;

myStruct.b = 'A';

  union MyUnion myUnion;

myUnion.x = 10;

myUnion.y = 'A';

printf("Struct values: %d, %c\n", myStruct.a, myStruct.b);

printf("Union values: %d, %c\n", myUnion.x, myUnion.y);

  return 0;

}

In the above code, we have defined a struct named MyStruct which contains an integer and a character, and a union named MyUnion which also contains an integer and a character. We then create instances of both MyStruct and MyUnion and set their values. We set the integer value of both to 10 and the character value of both to ‘A’.

When we print out the values of the struct and union, we can see that the struct has separate variables for the integer and character, and each variable retains its value. The output is shown below:

Conclausion

Structure and union are two different datatypes that have the similar syntax in C language. They are different in some aspects such as storage, memory allocation, usability, and other features. This article presents an easy guide to find a detailed comparison between structure and union followed by coding examples that clearly differentiate both in C programming.

Share Button

Source: linuxhint.com

Leave a Reply