Created
September 21, 2014 12:14
-
-
Save JesseEisen/4e5aac5dbcba3c7d1ccf to your computer and use it in GitHub Desktop.
This is a snippet to test a OS use which endian.
This file contains 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<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