【发布时间】:2011-08-06 06:39:31
【问题描述】:
这里是 MSDN 链接 来自http://msdn.microsoft.com/en-us/library/s3f49ktz(v=VS.80).aspx
它说: 无符号整数:4字节 值范围 0 到 4,294,967,295
因此我的测试代码:
void main(void)
{
unsigned int sum; //4byte, 32bit
sum = 2147483648; //2^31 represent by 1 followed by 31 0s
printf("sum is %d\n",sum);
sum = sum -1 ; //2^31-1 represent by 0 followed by 31 1s
printf("sum is %d\n",sum);
getchar();
}
我确定 4,294,967,295=2^32-1,printf 将打印“0”,转储最高有效位 我认为 MSDN 应该写值范围:0 到 2147483647 对吗?
【问题讨论】:
-
使用
%u而不是%d。后者用于打印带符号的 int。 -
您可能想使用
%u而不是%d,因为您正在尝试打印unsigned int。我认为这会消除你的一些困惑。 -
不,unsigned int 使用它的所有位。没有一个被“倾倒”。
-
这也许是一个更好的例子:ideone.com/rSR0C
-
main返回一个int。需要明确的是,MSDN 信息适用于特定的编译器和平台,所以这并不是普遍适用的。
标签: c++ c microprocessors