Skip to content

Instantly share code, notes, and snippets.

@JesseEisen
Created September 21, 2014 12:14
Show Gist options
  • Save JesseEisen/4e5aac5dbcba3c7d1ccf to your computer and use it in GitHub Desktop.
Save JesseEisen/4e5aac5dbcba3c7d1ccf to your computer and use it in GitHub Desktop.
This is a snippet to test a OS use which endian.
#include<stdio.h>
int main(int argc, char *argv[])
{
union
{
short s;
char c[sizeof(short)];
}un;
un.s = 0x0102;
if(sizeof(short) == 2)
{
if(c[0] == 1 && c[1] == 2)
printf("big-endia\n");
else if(c[0] == 2 && c[1] == 1)
printf("little-endian\n");
else
printf("unknow\n");
}
else
printf("sizeof(short) = %d\n",sizeof(short));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment