Search⌘ K
AI Features

Endianness and the Union

Explore the concept of endianness and its impact on data storage using unions in C programming. Understand how byte order varies between little-endian and big-endian systems, and how this affects memory representation in your code. This lesson helps you grasp processor-specific data handling for better program reliability.

Endianness


Endianness is a sequencing of bytes in a computer’s memory.


Let’s understand the union in more detail with the help of endianness. Have a look at the output of the code given below!

C
#include <stdio.h>
int main( )
{
// union declaration
union a
{
int i ;
char ch[ 4 ] ;
} ;
// Declares union variable
union a z ;
// Assign value to union member
z.i = 512 ;
// Print values of union members
printf ( "%d %d %d %d %d\n", z.i, z.ch[ 0 ], z.ch[ 1 ] , z.ch[ 2 ], z.ch[ 3 ] ) ;
}

The binary of 512 is ...