为什么unsigned int包含负数

时间:2022-11-11 01:42:39

What I know about unsigned numerics (unsigned short, int and longs), that It contains positive numbers only, but the following simple program successfully assigned a negative number to an unsigned int:

我所知道的unsigned数字(unsigned short,int和longs),它只包含正数,但是下面的简单程序成功地将一个负数分配给unsigned int:

  1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  prog4.c
  5  *
  6  * =====================================================================================
  7  */
  8 
  9 #include <stdio.h>
 10 
 11 int main(void){
 12 
 13     int v1 =0, v2=0;
 14     unsigned int sum;
 15     
 16     v1 = 10;
 17     v2 = 20;
 18     
 19     sum = v1 - v2;
 20     
 21     printf("The subtraction of %i from %i is %i \n" , v1, v2, sum);
 22     
 23     return 0;
 24 }

The output is : The subtraction of 10 from 20 is -10

输出为:从20减去10为-10

4 个解决方案

#1


22  

%i is the format specifier for a signed integer; you need to use %u to print an unsigned integer.

%i是有符号整数的格式说明符;你需要使用%u来打印无符号整数。

#2


8  

With printf, the %i format outputs a signed int. Use %u to output an unsigned int. This is a common issue when beginning C programming. To address your question, the result of v1 - v2 is -10, but sum is an unsigned int, so the real answer is probably something like 4294967286 (232 - 10). See what you get when you use The subtraction of %i from %i is %u \n. :)

对于printf,%i格式输出一个signed int。使用%u输出unsigned int。这是开始C编程时的常见问题。为了解决你的问题,v1 - v2的结果是-10,但sum是unsigned int,所以真正的答案可能是4294967286(232 - 10)。看看你使用时得到的东西从%i减去%i是%u \ n。 :)

#3


6  

Signed int and unsigned int are the same size in memory, the only difference between them is how you intepret them. Signed values use a twos complement representation.

Signed int和unsigned int在内存中的大小相同,它们之间的唯一区别就是你如何解释它们。有符号值使用二进制补码表示。

If you put 0xFFFFFFFF in a 4 byte memory location, and then ask what is the value in there? Well if we interpret it as a signed int, then it is -1, but if we interpret it as an unsigned int then the value is 4294967295. Either way it's the same bit pattern, the difference is what meaning you give it.

如果你把0xFFFFFFFF放在一个4字节的内存位置,然后问那里的值是多少?好吧,如果我们把它解释为有符号的int,那么它是-1,但是如果我们把它解释为unsigned int那么值是4294967295.无论哪种方式它都是相同的位模式,差异就是你赋予它的含义。

When you assigned 10 - 20 into an unsigned int, you calculated a value of -10 (C doesn't do overflow or underflow checking), that's a bit pattern of 0xFFFFFFF6, which means -10 in a signed int or 4294967286 in an unsigned int. If you then tell the compiler (by using %i) to print a signed int then it interprets that bit pattern as a signed int and prints -10, if you told the compiler (by using %u) to print an unsigned int then it interprets that bit pattern as unsigned and prints 4294967286.

当你将10 - 20分配给unsigned int时,你计算了一个值-10(C不进行上溢或下溢检查),这是0xFFFFFFF6的位模式,这意味着在有符号的int中为-10或在无符号中为4294967286 INT。如果然后告诉编译器(通过使用%i)打印signed int,那么它将该位模式解释为signed int并打印-10,如果你告诉编译器(通过使用%u)打印unsigned int然后它将该位模式解释为unsigned并打印4294967286。

#4


1  

Because unsigned int value that is stored in sum is treated like signed decimal integer in printf %i

因为存储在sum中的unsigned int值被视为printf%i中的带符号十进制整数

#1


22  

%i is the format specifier for a signed integer; you need to use %u to print an unsigned integer.

%i是有符号整数的格式说明符;你需要使用%u来打印无符号整数。

#2


8  

With printf, the %i format outputs a signed int. Use %u to output an unsigned int. This is a common issue when beginning C programming. To address your question, the result of v1 - v2 is -10, but sum is an unsigned int, so the real answer is probably something like 4294967286 (232 - 10). See what you get when you use The subtraction of %i from %i is %u \n. :)

对于printf,%i格式输出一个signed int。使用%u输出unsigned int。这是开始C编程时的常见问题。为了解决你的问题,v1 - v2的结果是-10,但sum是unsigned int,所以真正的答案可能是4294967286(232 - 10)。看看你使用时得到的东西从%i减去%i是%u \ n。 :)

#3


6  

Signed int and unsigned int are the same size in memory, the only difference between them is how you intepret them. Signed values use a twos complement representation.

Signed int和unsigned int在内存中的大小相同,它们之间的唯一区别就是你如何解释它们。有符号值使用二进制补码表示。

If you put 0xFFFFFFFF in a 4 byte memory location, and then ask what is the value in there? Well if we interpret it as a signed int, then it is -1, but if we interpret it as an unsigned int then the value is 4294967295. Either way it's the same bit pattern, the difference is what meaning you give it.

如果你把0xFFFFFFFF放在一个4字节的内存位置,然后问那里的值是多少?好吧,如果我们把它解释为有符号的int,那么它是-1,但是如果我们把它解释为unsigned int那么值是4294967295.无论哪种方式它都是相同的位模式,差异就是你赋予它的含义。

When you assigned 10 - 20 into an unsigned int, you calculated a value of -10 (C doesn't do overflow or underflow checking), that's a bit pattern of 0xFFFFFFF6, which means -10 in a signed int or 4294967286 in an unsigned int. If you then tell the compiler (by using %i) to print a signed int then it interprets that bit pattern as a signed int and prints -10, if you told the compiler (by using %u) to print an unsigned int then it interprets that bit pattern as unsigned and prints 4294967286.

当你将10 - 20分配给unsigned int时,你计算了一个值-10(C不进行上溢或下溢检查),这是0xFFFFFFF6的位模式,这意味着在有符号的int中为-10或在无符号中为4294967286 INT。如果然后告诉编译器(通过使用%i)打印signed int,那么它将该位模式解释为signed int并打印-10,如果你告诉编译器(通过使用%u)打印unsigned int然后它将该位模式解释为unsigned并打印4294967286。

#4


1  

Because unsigned int value that is stored in sum is treated like signed decimal integer in printf %i

因为存储在sum中的unsigned int值被视为printf%i中的带符号十进制整数