#include<stdio.h>
#define is_little_endian3(x) *(char *)&x
union endian_un
{
short var;
char bits[sizeof(short)];
};
int is_little_endian1()
{
union endian_un flag;
flag.var=0x0102;
if(flag.bits[0]==1&&flag.bits[1]==2)
return 0;
else
return 1;
}
int is_little_endian2()
{
unsigned short flag=0x4321;
if(*(char *)&flag==0x21)
return 1;
else
return 0;
}
int main()
{
if(is_little_endian1())
printf("judge by method 1,little-endian\n");
else
printf("judge by method 1,big-endian\n");
if(is_little_endian2())
printf("judge by method 2,little-endian\n");
else
printf("judge by method 2,big-endian\n");
short a=0x00ff;
if(is_little_endian3(a))
printf("judge by method 3,little-endian\n");
else
printf("judge by method 3,big-endian\n");
return 0;
}
|