#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");
}
转载请注明出处,谢谢!