尝试打印UINT_MAX时得到-1 [英] When trying to print UINT_MAX I get -1

查看:176
本文介绍了尝试打印UINT_MAX时得到-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试全部打印'UINT_MAX'时,我得到-1,这是为什么?这是我的"main()"中唯一的东西,它是一个打印"UINT_MAX"的printf语句

When trying to print 'UINT_MAX' all I get it -1, why is this? It's the only thing I have in my 'main()', a printf statement that prints 'UINT_MAX'

推荐答案

您可以使用printf()打印它

You can print it with printf()

printf("%u\n", UINT_MAX);

您需要使用u说明符, man printf .

为什么用d说明符得到-1?这是因为内存中有符号整数的工作方式.

Why you get -1 with d specifier? It's because of how work a signed integer in memory.

unsigned integer从0开始,在内存中是一个简单的二进制系统.

unsigned integer start at 0, in memory it a simple binary system.

十进制=>二进制

  • 1 => 1
  • 2 => 10
  • 8 => 1000
  • 255 => 11111111

带符号整数使用一位被解释为负数,这令人困惑,因为将无符号整数像整数一样解释时的最大值是-1.

signed integer use a bit to be interpreted as a negative number, it's confusing because the max value of an unsigned int when it is interpreted like an int is -1.

具有8位整数的示例:

  • -1 => 11111111
  • -128 => 10000000
  • -9 => 11110111

就像您看到的那样,第一位曾经是符号位.

Like you see, the first bit is used to be the sign bit.

尝试使用此网站

这篇关于尝试打印UINT_MAX时得到-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆