*大端小端由機器CPU決定。**ide
大端
低位高地址,高位低地址。函數
小端
低位低地址,高位高地址。code
#include<stdio.h> union un { int a; char b; }u; void main() { u.a = 1;//既賦值給 printf("%d\n", u.b); //a 0000 0000 0000 0000 0000 0000 0000 0001 //b 0000 0001 //b取的是低地址的8位,存在1則說明a的存儲是低位存儲在低地址,則爲小端。 }
#include<stdio.h> int fun() { int a = 1; char *p = (char*)&a;//取四個字節的低地址的一個字節; return *p; //a的1值存放在低地址仍是高地址; } void main() { char c=fun(); if (c = 1) //char取低地址的一個字節 printf("小端\n"); else printf("大端\n"); }