C语言 C 中的无符号值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32344810/
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
Unsigned values in C
提问by rvillablanca
I have the following code:
我有以下代码:
#include <stdio.h>
int main() {
unsigned int a = -1;
int b = -1;
printf("%x\n", a);
printf("%x\n", b);
printf("%d\n", a);
printf("%d\n", b);
printf("%u\n", a);
printf("%u\n", b);
return 0;
}
The output is:
输出是:
ffffffff
ffffffff
-1
-1
4294967295
4294967295
I can see that a value is interpreted as signed or unsigned according to the value passed to printffunction. In both cases, the bytes are the same (ffffffff). Then, what is the unsignedword for?
我可以看到根据传递给printf函数的值,一个值被解释为有符号或无符号。在这两种情况下,字节都是相同的 ( ffffffff)。那么,这个unsigned词是什么?
采纳答案by chux - Reinstate Monica
Assign a int -1to an unsigned: As -1does not fit in the range [0...UINT_MAX], multiples of UINT_MAX+1are added until the answer is in range. Evidently UINT_MAXis pow(2,32)-1 or 429496725on OP's machine so ahas the value of 4294967295.
将 a 分配int -1给 an unsigned:由于-1不适合范围内[0...UINT_MAX],因此UINT_MAX+1会添加 的倍数,直到答案在范围内。显然UINT_MAX是pow(2,32)-1 or 429496725在 OP 的机器上,所以a它的值为4294967295。
unsigned int a = -1;
The "%x", "%u"specifier expects a matching unsigned. Since these do not match, "If a conversion specification is invalid, the behavior is undefined.
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined." C11 §7.21.6.1 9. The printf specifier does not change b.
的"%x","%u"符预期匹配unsigned。由于这些不匹配,“如果转换规范无效,则行为未定义。如果任何参数不是相应转换规范的正确类型,则行为未定义。” C11 §7.21.6.1 9. printf 说明符没有改变b。
printf("%x\n", b); // UB
printf("%u\n", b); // UB
The "%d"specifier expects a matching int. Since these do not match, more UB.
在"%d"符预期的匹配int。由于这些不匹配,更多UB。
printf("%d\n", a); // UB
Given undefined behavior, the conclusions are not supported.
鉴于未定义的行为,不支持结论。
both cases, the bytes are the same (ffffffff).
两种情况下,字节都是相同的(ffffffff)。
Even with the same bit pattern, different types may have different values. ffffffffas an unsignedhas the value of 4294967295. As an int, depending signed integer encoding, it has the value of -1, -2147483647 or TBD. As a floatit may be a NAN.
即使具有相同的位模式,不同的类型也可能具有不同的值。ffffffffas anunsigned的值为 4294967295。作为一个int,取决于有符号整数编码,它的值为 -1、-2147483647 或 TBD。因为float它可能是一个NAN。
what is unsigned word for?
什么是未签名的词?
unsignedstores a whole number in the range [0 ... UINT_MAX]. It never has a negative value. If code needs a non-negative number, use unsigned. If code needs a counting number that may be +, - or 0, use int.
unsigned存储范围内的整数[0 ... UINT_MAX]。它永远不会有负值。如果代码需要非负数,请使用unsigned. 如果代码需要一个可能是 +、- 或 0 的计数数字,请使用int.
Update: to avoid a compiler warning about assigning a signed intto unsigned, use the below. This is an unsigned1ubeing negated - which is well defined as above. The effect is the same as a -1, but conveys to the compiler direct intentions.
更新:为了避免编译器警告有关分配签署int到unsigned,使用下面。这是一个unsigned1u被否定的——如上定义。效果与 a 相同-1,但传达给编译器的直接意图。
unsigned int a = -1u;
回答by Donotalo
Having unsignedin variable declaration is more useful for the programmers themselves - don't treat the variables as negative. As you've noticed, both -1and 4294967295have exact same bit representation for a 4 byte integer. It's all about how you want to treator seethem.
有unsigned在变量声明是程序员自己更有用-不要把变量为负。正如你已经注意到了,无论是-1和4294967295有一个4字节的整数完全相同的位表示。这完全取决于您想如何对待或看到他们。
The statement unsigned int a = -1;is converting -1in two's complement and assigning the bit representation in a. The printf()specifier x, dand uare showing how the bit representation stored in variable alooks like in different format.
该语句unsigned int a = -1;正在转换-1为二进制补码并在 中分配位表示a。的printf()说明符x,d和u被示出如何存储在变量中的位表示a看起来像在不同的格式。
回答by Santosh A
When you initialize unsigned int a to -1;it means that you are storing the 2'scomplement of -1into the memory of a.
Which is nothing but 0xffffffffor 4294967295.
当您初始化时,unsigned int a to -1;这意味着您将 的2's补码存储-1到 的内存中a。
这只是0xffffffff或4294967295。
Hence when you print it using %x or %uformat specifier you get that output.
因此,当您使用%x or %u格式说明符打印它时,您会得到该输出。
By specifying signedness of a variable to decide on the minimum and maximum limit of value that can be stored.
通过指定变量的符号来决定可以存储的值的最小和最大限制。
Like with unsigned int: the range is from 0 to 4,294,967,295and int: the range is from -2,147,483,648 to 2,147,483,647
就像unsigned int:范围是从0 to 4,294,967,295和int:范围是从-2,147,483,648 to 2,147,483,647
For more info on signedness refer this
有关签名的更多信息,请参阅此
回答by ARAVINTHKUMAR J
In the hexadecimalit can't get a negative value. So it shows it like ffffffff.
在十六进制中它不能得到负值。所以它显示为ffffffff。
The advantage to using the unsigned version (when you know the values contained will be non-negative) is that sometimes the computer will spot errors for you (the program will "crash" when a negative valueis assigned to the variable).
使用无符号版本(当您知道所包含的值将是非负值时)的优点是有时计算机会为您发现错误(当将负值分配给变量时,程序将“崩溃” )。

