代码如下:
big_little_endian.c
1 #include <stdio.h> 2 3 #define BIG_ENDIAN 1 4 #define LITTEL_ENDIAN 0 5 6 int big_littel_endian(void) 7 { 8 union num 9 { 10 int a; 11 char b[4]; 12 }c; 13 14 c.a = 0x1234; 15 16 if(c.b[0] == 0x34) 17 { 18 return LITTEL_ENDIAN; 19 } 20 else 21 { 22 return BIG_ENDIAN; 23 } 24 25 } 26 27 int main() 28 { 29 int cpu; 30 31 cpu = big_littel_endian(); 32 33 if(cpu == LITTEL_ENDIAN) 34 printf("cpu is Little_endian.\n"); 35 else 36 printf("cpu is Big_endian.\n"); 37 }
在Linux下编译:
gcc -o big_little_endian big_little_endian.c
运行:
./big_little_endian
输出结果:
cpu is Little_endian.
时间: 2024-10-18 17:23:29