#include<stdio.h>
int main(void){
int a = -12;
int mSize = sizeof(a);
printf("a=%d, sizeof(a)=%d.\n", a, mSize);
printf("size of int is %ld.\n", sizeof(int));
unsigned char *p = (unsigned char*) &a; //一定要用unsigned char * !!!
printf("a in memory: ");
for(int i=0; i<mSize; i++){
printf("%02x ", *(p+i)); //按十六进制输出,占两个字符宽度,补0
}
printf("\n");
return 0;
}
在我电脑上运行的结果:
a=-12, sizeof(a)=4.
size of int is 4.
a in memory: f4 ff ff ff
解释:int类型大小为4字节,系统采用小端法,故实际顺序为ff ff ff f4,取反后为00 00 00 0b,加1后为00 00 00 0c,即-12。