본문 바로가기

IT Tech/Programming

[C언어] Endian 체크

반응형





#define LITTLE_ENDIAN   1
#define BIG_ENDIAN      2
#define PDP_ENDIAN      3

#1
int get_endian()
{
     static long int str[2] = { 0x41424344,0x0 }; /* ASCII "ABCD" */

     if (strcmp("DCBA", (char *) str) == 0)
         return LITTLE_ENDIAN;

     else if (strcmp("ABCD", (char *) str) == 0)
        return BIG_ENDIAN;

     else if (strcmp("BADC", (char *) str) == 0)
        return PDP_ENDIAN;
}



#2 int endian(void) { int i = 0x00000001; if ( ((char *)&i)[0] ) return LITTLE_ENDIAN; else return BIG_ENDIAN; }


반응형