nixawk / hello-c

c programming
9 stars 11 forks source link

c - little endion VS big endion #6

Open nixawk opened 7 years ago

nixawk commented 7 years ago
#include <stdio.h>

typedef int bool;
#define TRUE 1
#define FALSE 0

typedef unsigned char *byte_ptr;

bool is_little_endian(void)
{
    unsigned int x = 0x01234567;
    return (*((byte_ptr)&x) == 0x01) ? FALSE : TRUE;
}

int
main(void)
{
    printf("%s\n", is_little_endian() ? "little endian" : "big endian");
    return 0;
}

References

  1. https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html
  2. http://www.geeksforgeeks.org/little-and-big-endian-mystery/