#include <stdio.h>
#ifndef BOOL
typedef enum BOOL_type {FALSE=0, TRUE=1} BOOL;
#endif
#ifndef uchar
typedef unsigned char uchar;
#endif
#ifndef ushort
typedef unsigned short int ushort;
#endif
BOOL isSmallEndianSys(void);
void main(void)
{
printf("System Endian order test: %s Endian\n",
isSmallEndianSys() ? "Small" : "Big");
}
BOOL isSmallEndianSys(void)
{
union {
uchar LowByte;
ushort Short;
} TestEndian;
TestEndian.Short = 0x0001;
return ((0x01 == TestEndian.LowByte) ? TRUE : FALSE);
}
上述代碼可能和網絡上的一致或略有區別,爲原創代碼:
還有個超級精簡版(我的原創):
#include <stdio.h>
void main(void)
{
const unsigned short int num = 0x0001;
printf("System Endian order test: %s Endian\n",
*((unsigned char *) &num) ? "Small" : "Big");
}
轉載請註明出處,謝謝!