echo '' ;

C – structures and unions

In C programming, structures (struct) and unions (union) are fundamental data types used to organize and manipulate related data. They both allow grouping multiple variables of different data types into a single entity. However, they have distinct characteristics and usage scenarios.

A structure (struct) is a composite data type that enables bundling together variables of different types under a single name. Structures commonly use to represent records, objects, or complex data structures, and each variable within a structure is called a member, which can be of any data type, including other structures or arrays.

On the other hand, a union (union) is similar to a structure but with a key difference: all members of a union share the same memory location. This means that a union variable can hold only one value at a time, regardless of the number of members it has. Programmers often use unions when memory efficiency is crucial or when different data types need to share the same memory space.

In this introduction to “C – struct and union,” we will explore the syntax, usage, and differences between structures and unions in C, along with common scenarios where each data type is appropriate. We will also discuss best practices and potential pitfalls associated with using structures and unions in C programming.

differences between a union and a structure:

FeatureUnionStructure
DefinitionA union is a data type that allows storing different types of data in the same memory location.A structure is a composite data type that allows storing different types of data in a single variable.
Memory UsageOccupies memory space equal to the size of the largest member.Occupies memory space equal to the sum of the sizes of its members.
Data SharingShares memory among its members.Each member has its own memory space.
Accessing MembersAll members share the same memory location, so only one member can be accessed at a time.Each member has its own memory location, and all members can be accessed independently.
Use CasesUseful when different data types need to share the same memory location to conserve memory.Suitable for grouping related data together, such as representing a record or object.
  • The difference between structure and union is, … The amount of memory required to store a structure variable is the sum of the size of all the members.
  • On the other hand, in case of unions, the amount of memory required is always equal to that required by its largest member.

Example -1 : struct inside union.

Consider the following C declaration, Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)

struct { 
    short s[5];
    union { 
         float y; 
         long z; 
    }u; 
}

Answer : 18

Short array s[5] will take 10 bytes as size of short is 2 bytes. When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).


NEXT

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading