热度 9
2013-11-7 13:13
1324 次阅读|
0 个评论
以下程序是在同一台式机上vc6测试: ----------------------------------------- //输出不同类型所占内存空间长度 #include "stdio.h" int main() { printf("char length:%d\n",sizeof(char)); printf("short length:%d\n",sizeof(short)); printf("int length:%d\n",sizeof(int)); printf("long length:%d\n",sizeof(long)); return 1; } //char length:1 //short length:2 //int length:4 //long length:4 //Press any key to continue ----------------------------------------- //判断芯片的大端小端 #include "stdio.h" int main() { union w { int a; //4 bytes char b;//1 byte }c; c.a=1; if (c.b==1) { printf("It is Little_endian!\n"); } else { printf("It is Big_endian!\n"); } return 1; } //It is Little_endian! //Press any key to continue ----------------------------------------- //小端模式低地址存放最低位 #include "stdio.h" int main() { int aa = 0x12345678; printf("%x\n",aa); printf("%x\n",*(char *)(aa)); printf("%x\n",*((char *)(aa)+1)); printf("%x\n",*((char *)(aa)+2)); printf("%x\n",*((char *)(aa)+3)); return 1; } //12345678 //78 //56 //34 //12 //Press any key to continue ----------------------------------------- //小端模式结构体中先定义的成员地址最低 #include "stdio.h" int main() { typedef struct __BigSmallEndian { char a; char b; char c; char d; }BigSmallEndian; BigSmallEndian Check; printf("Check Address:%x\n",Check); printf("Check.a Address:%x\n",(Check.a)); printf("Check.b Address:%x\n",(Check.b)); printf("Check.c Address:%x\n",(Check.c)); printf("Check.d Address:%x\n",(Check.d)); return 1; } //Check Address:18ff44 //Check.a Address:18ff44 //Check.b Address:18ff45 //Check.c Address:18ff46 //Check.d Address:18ff47 //Press any key to continue ----------------------------------------- #include "stdio.h" int main(int argc, char *argv ) { char array = {'a','b',0,'A','B'}; printf("arrar Address:%x ",array );printf("arrar value:%x\n",array ); printf("arrar Address:%x ",array );printf("arrar value:%x\n",array ); printf("arrar Address:%x ",array );printf("arrar value:%x\n",array ); printf("arrar Address:%x ",array );printf("arrar value:%x\n",array ); printf("arrar Address:%x ",array );printf("arrar value:%x\n",array ); } //arrar Address:18ff40 arrar value:61 //arrar Address:18ff41 arrar value:62 //arrar Address:18ff42 arrar value:0 //arrar Address:18ff43 arrar value:41 //arrar Address:18ff44 arrar value:42 //Press any key to continue