代码一:联合体union的存放顺序是所有成员都从低地址开始存放,若处理器是Big_endian(从高字节到低字节存放数据)的,则返回0;若是Little_endian(从低字节到高字节存放数据)的,则返回1。
int checkCPU() { union w { int a; char b; } c; c.a = 1; return (c.b == 1); }
代码二: #include <stdio.h> #include <stdlib.h> int main() {
int n = 0X11223344; char *p = (char *)&n; if(*p == 0X11) { printf("Big Endian "); } else if(*p == 0X44) { printf("Little Endian "); } else { printf("Impossible "); } system("pause"); return 0;