您的位置:首页 > 其它

系统大小端测试程序

2015-06-17 12:56 281 查看
#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");
} 

转载请注明出处,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: