Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created January 23, 2024 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmccarthy/2e98b82fe269d09526ec50f89593b3f5 to your computer and use it in GitHub Desktop.
Save ljmccarthy/2e98b82fe269d09526ec50f89593b3f5 to your computer and use it in GitHub Desktop.
C program to detect machine endianness
#include <stdint.h>
#include <stdio.h>
int main(void)
{
union { uint8_t bytes[4]; uint32_t value; } x = { .bytes = {0x11, 0x22, 0x33, 0x44} };
switch (x.value) {
case 0x11223344:
printf("big\n");
return 0;
case 0x44332211:
printf("little\n");
return 0;
case 0x22114433:
printf("middle\n");
return 0;
default:
printf("unknown\n");
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment