Unsigned Int 的最大值是 -1

eLg*_*eLg -1 c unsigned types unsigned-long-long-int unsigned-integer

我试图找出无符号数据类型的最小值和最大值。我知道最小无符号值是 0,最大值是 (2^n)-1。但是,当我尝试运行我的程序时(我无法发布我的代码,但您可以参考),我一直将 -1 作为最大值。有人可以向我解释为什么吗?另外,UINT_MAX 给我 4294967295,而 ULLONG_MAX 是 4294967295。但是,unsigned int 的最大值应该是 65535,而 unsigned long long int 应该是 +18,446,744,073,709,551,615。为什么输出不同?

sam*_*ari 6

你用来打印这些值的格式说明符是什么?这类错误主要是由于错误的格式说明符而发生的。

#include <stdio.h>
#include <limits.h>

int main()
{  
    printf("%u", UINT_MAX); // This will print 4294967295 (system dependent)
    printf("%d", UINT_MAX); // This will print -1
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • `printf("%d", UINT_MAX); // 这将打印 -1` 这也是系统相关的,因为并非每个系统都是二进制补码。 (2认同)
  • @ChristianGibbons:虽然理论上可以存在非二进制补码机器,但您不会在博物馆外找到机器。 (2认同)