C语言 C中的无符号整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15340415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:39:43  来源:igfitidea点击:

unsigned integers in C

cunsigned-integer

提问by amitshree

While i am running the program below it outputs like 109876543210-1-2-3-4-5-6-78-9-10-11-12-and s0 on. Why so? What is the concept of unsigned integer?

当我在下面运行程序时,它会输出 109876543210-1-2-3-4-5-6-78-9-10-11-12-and s0 on。为什么这样?无符号整数的概念是什么?

 main ()
   {
      unsigned int i;
       for (i = 10; i >= 0; i--)
                 printf ("%d", i);
   }

回答by Vicky

Unsigned integers are always non-negative, that is greater than or equal to 0. When you subtract 1 from 0 in an unsigned integer type you end up with MAX_INT.

无符号整数总是非负的,即大于或等于 0。当你在无符号整数类型中从 0 中减去 1 时,你最终会得到 MAX_INT。

Therefore, your for loop will never terminate.

因此,您的 for 循环永远不会终止。

However, you need to use "%u" not "%d" in your printf if you want it to print the unsigned value rather than the signed value.

但是,如果您希望它打印无符号值而不是有符号值,则需要在 printf 中使用“%u”而不是“%d”。

回答by Vicky

Why so?

为什么这样?

Because your program has an undefined behavior (using %dwhich is for intto print an unsigned) - so you can expect anything to happen.

因为您的程序具有未定义的行为(使用%dwhich 用于int打印unsigned) - 所以您可以期待任何事情发生。

Why the infinite loop? Because an unsigned intis always >= 0.

为什么是无限循环?因为 anunsigned int总是>= 0

What is the concept of unsigned integer?

无符号整数的概念是什么?

It's an... unsignedinteger. A whole number which is non-negative.

这是一个……无符号整数。一个非负的整数。

回答by David Heffernan

The %dformat string treats the substituted value as int, which is signed.

%d格式串对待取代的值作为int,其被签署。

回答by Aneri

You use %d, so printf interprets the value as signed. Comparison to zero (>=0) of unsigned is always true.

您使用%d,因此 printf 将值解释为有符号。与 unsigned 的零 (>=0) 的比较始终为真。

So, while values are from 10 to 0, the output is ok (109876543210). After that the value becomes huge positive (maximum value, for 32bit int it is 0xFFFFFFFF). Comparison to 0 is true, so the loop continues. But in printf0xFFFFFFFFproduces -1since %dis used. Then the loop continues to 0xFFFFFFFEwhich is -2but still >= 0as unsigned.

因此,虽然值从 10 到 0,但输出没问题 (109876543210)。之后该值变为巨大的正数(最大值,对于 32 位 int 是0xFFFFFFFF)。与 0 的比较为真,因此循环继续。但在printf0xFFFFFFFF生产-1%d使用。然后循环继续0xFFFFFFFE-2但仍为>= 0无符号。

Use %u.

使用%u.

回答by nemetroid

printfcan't know the type of the variable you give it, all it gets is the value (the bits themselves). You have to tell it how to interpret that value, and with %dyou tell it to interpret it as a signed integer.

printf无法知道你给它的变量的类型,它得到的只是值(位本身)。您必须告诉它如何解释该值,并%d告诉它将其解释为有符号整数。

回答by Andreas Fester

You are using "%d" with printf() which is the format specifier for a signed integer.

您正在将 "%d" 与 printf() 一起使用,它是有符号整数的格式说明符。

回答by Steve Valliere

Signed integers (we'll use 16 bit) range from -32768 to 32767 (0x8000 to 0x7FFF) while unsignedintegers range from 0 to 65535 (0x0000 to 0xFFFF). So unsigned integers cannot have negative values, which is why your loop never terminates. However, you have told the print program to format the output as ifit were signed(%d) so that you see the numbers formatted as negative values in your output. At some level, everything in a computer is just a string of bits that needs interpretation and your code example uses two different interpretations of the same bit patterns ... your unsigned int.

有符号整数(我们将使用 16 位)范围从 -32768 到 32767(0x8000 到 0x7FFF),而无符号整数范围从 0 到 65535(0x0000 到 0xFFFF)。所以无符号整数不能有负值,这就是为什么你的循环永远不会终止。但是,你已经告诉打印程序格式化输出,就好像它被签署%d),这样你看到格式化为在输出负值的数字。在某种程度上,计算机中的一切都只是一串需要解释的位,您的代码示例使用相同位模式的两种不同解释……您的无符号整数。

回答by Dan

You should use %uas a format specifierin the printf, otherwise the value is casted to int.

您应该在 printf 中%u用作格式说明符,否则该值将被强制转换为int.

回答by onon15

The declaration unsigned integerinstructs the compiler to use unsigned operations on the variable. For example, the >>operator has different behavior on unsigned integers vs signed (specifically, whether it should preserve the sign bit).

该声明unsigned integer指示编译器对变量使用无符号运算。例如,>>运算符对无符号整数和有符号整数有不同的行为(具体来说,它是否应该保留符号位)。

To print an unsigned integer, you should use the %uformatting.

要打印无符号整数,您应该使用%u格式。