...

/

Endianness and the Union

Endianness and the Union

Get introduced to the endianness, i.e., little-endian and big-endian storage.

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!

Press + to interact
#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 00000000 00000000 ...