系統大小端測試程序

#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");
} 

轉載請註明出處,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章