X-man

导航

《程序员面试宝典》强制转换,内存地址

1.无符号整形输出

#include<stdio.h>
int main()
{
    unsigned int a=0xffffffff;
    printf("%u\n",a);
}

不能用%d输出。

#include<stdio.h> 
void main()
{
    unsigned int a = 0xAAAAAAA7;
    unsigned char i = (unsigned char)a;
    char* b = (char*)&a;
    unsigned char* c = (unsigned char*)&a;
    //char 1个字节 unsigned char 1个字节
    printf("%d %d\n",sizeof(char),sizeof(unsigned char));
    printf("%d %d %d %d\n",sizeof(unsigned int),sizeof(unsigned char),sizeof(char *),sizeof(unsigned char *));
    printf("% 8x, % 8x, % 3x,% 8x\n", a, i, *b, *c);
}
输出:
1
1 4 1 4 4 aaaaaaa7, a7, ffffffa7, a7

b或者c是指向a的内存,但是只是指向a的低字节内存。也就是0xA7。b是char*类型,所以把0xA7解释为负数,所以*b输出结果为0xffffffa7,等于-89。而c,为unsigned char*类型,*c的输出为167。

posted on 2014-08-06 20:02  雨钝风轻  阅读(293)  评论(0编辑  收藏  举报