Created
October 31, 2015 22:26
-
-
Save nakal/92ddf81fbd828c873d94 to your computer and use it in GitHub Desktop.
Endianness test for C (untested)
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 is_little_endian(void) | |
{ | |
char buf[sizeof(unsigned int)]; | |
*(unsigned int *)buf = 1u; | |
return (int)buf[0]; | |
} | |
int main(void) | |
{ | |
printf("This host is %s endian.\n", | |
is_little_endian() ? "little" : "big"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment