Created
January 23, 2024 07:15
-
-
Save ljmccarthy/2e98b82fe269d09526ec50f89593b3f5 to your computer and use it in GitHub Desktop.
C program to detect machine endianness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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