← Back to davo.co
aboutsummaryrefslogtreecommitdiffstats
path: root/endianness.c
blob: ec589d736930de977505b5e596e09b5fdf7909f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <inttypes.h>

typedef union unsignedInspect unsignedInspect;
union unsignedInspect {
  unsigned val;
  unsigned char bytes[sizeof(unsigned)];
};
unsignedInspect twofold = { .val = 0xAABBCCDD, };

int main(void) {
  printf("value is 0x%.08X\n", twofold.val);
  for (size_t i = 0; i < sizeof twofold.bytes; ++i)
    printf("byte[%zu]: 0x%.02hhX\n", i, twofold.bytes[i]);
  unsigned val = 0xAABBCCDD;
  unsigned char* valp = (unsigned char*)&val;
  for (size_t i = 0; i < sizeof val; ++i)
    printf("byte[%zu]: 0x%.02hhX\n", i, valp[i]);
}