读书人

一个折腾了挺久的有关问题

发布时间: 2012-11-04 10:42:42 作者: rapoo

一个折腾了挺久的问题
#include<stdio.h>

int main()

{
short x=~0xff;
printf("%.2x",x);
return 0;
}

我用GCC编译,具体版本没关注。。。为什么答案是0xffffff00
在我理解,应该是0Xff00 因为short是2位的,我用sizeof()确定过了。
在我看来,也许疑问就在printf("%.2x")上。。。这个用法是在《深入理解计算机系统》的书上看到的。在书上的解释是“C格式化指令“%.2x”表明整数必须用至少两个字的十六进制格式输出”。。。。。。。
请各位能否好好解释下结果为什么是0Xffffff00...


[解决办法]
print type:
x int Unsigned hexadecimal integer, using "abcdef."

so printf output int, it is no relation with your input short type.

读书人网 >C语言

热点推荐