Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created October 7, 2016 09:49
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 RklAlx/a46b1d67696124de1287efeb18b79eb7 to your computer and use it in GitHub Desktop.
Save RklAlx/a46b1d67696124de1287efeb18b79eb7 to your computer and use it in GitHub Desktop.
// https://helloacm.com/how-to-find-out-whether-a-machine-is-big-endian-or-little-endian-in-cc/
#include <stdio.h>
 
#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1
 
int TestByteOrder() {
        short int word = 0x0001;
        char *b = (char *)&word;
        return (b[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}
 
int main() {
        int r = TestByteOrder();
        printf("%s\n", r == LITTLE_ENDIAN ? "Little Endian" : "Big Endian");
        return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment