#include <stdio.h>
#include <math.h>
#include <limits.h>
int main(void)
{
unsigned long x = 0;
x = x ^ ~x;
printf("%d\n", x);
x = (unsigned long)pow(2, sizeof(x)*8);
printf("%d\n", x);
x = ULONG_MAX;
printf("%d\n", x);
return 0;
}
我在 Windows 7 上使用 CodeBlocks12.11 和 MinGW 4.7.0-1。由于某种原因,我无法让我的变量 x
获取最大可能的十进制值表示形式。为什么会发生这种情况,我确信 x = ULONG_MAX
应该有效,但它也会导致 -1
,现在肯定是不对的!我也尝试在代码块之外编译它。
我在这里错过了什么?
最佳答案
您必须使用u
打印无符号变量。 long 以 l
为前缀,因此在这种情况下您需要 lu
。
printf("%lu\n", x);
关于c - 如果无符号类型不应该有负值,为什么当我打开所有位时它是负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713575/