测试处理器是大端序还是小端序(Big Endian or Little Endian)

/* How can I determine whether a machine'sbyte orderis big-endian or little-endian? */
#include <stdio.h>
int main(int argc, char const *argv[])
{
	/*method 1*/
	int x = 1;
	if(*(char *)&x == 1)
		printf("little-endian\n");
	else	
		printf("big-endian\n");

	/*method 2*/
	{	
		union {
			int i;
			char c[sizeof(int)];
		} unx;
		unx.i = 1;
		if(unx.c[0] == 1)
			printf("little-endian\n");
		else	
			printf("big-endian\n");
	}
	return 0;
}

转自: http://c-faq.com/misc/endiantest.html

你可能感兴趣的:(测试处理器是大端序还是小端序(Big Endian or Little Endian))