为什么 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 }
输出为: 20 减 10 为 -10
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
%i
是有符号整数的格式说明符;您需要使用%u
来打印无符号整数。%i
is the format specifier for a signed integer; you need to use%u
to print an unsigned integer.对于
printf
,%i
格式输出一个signed int
。使用%u
输出一个unsigned int
。这是开始 C 编程时的常见问题。为了解决你的问题,v1 - v2
的结果是-10,但是sum
是一个unsigned int
,所以真正的答案可能是例如 4294967286 (232 - 10)。看看使用从 %i 减去 %i 得到的结果是 %u \n
。 :)With
printf
, the%i
format outputs asigned int
. Use%u
to output anunsigned int
. This is a common issue when beginning C programming. To address your question, the result ofv1 - v2
is -10, butsum
is anunsigned int
, so the real answer is probably something like 4294967286 (232 - 10). See what you get when you useThe subtraction of %i from %i is %u \n
. :)有符号整型和无符号整型在内存中的大小相同,它们之间的唯一区别在于解释它们的方式。有符号值使用二进制补码表示。
如果你把 0xFFFFFFFF 放在一个 4 字节的内存位置,然后问里面的值是多少?如果我们将其解释为有符号 int,则它是 -1,但如果我们将其解释为无符号 int,则该值为 4294967295。无论哪种方式,它都是相同的位模式,区别在于您赋予它的含义。
当您将 10 - 20 分配给无符号 int 时,计算出的值为 -10(C 不执行上溢或下溢检查),这是 0xFFFFFFF6 的位模式,这意味着有符号 int 中为 -10 或无符号 int 中为 4294967286国际。如果您然后告诉编译器(通过使用 %i)打印一个有符号 int 那么它会将该位模式解释为有符号 int 并打印 -10,如果您告诉编译器(通过使用 %u)打印一个无符号 int 那么它将该位模式解释为无符号并打印 4294967286。
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.
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.
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.
因为存储在 sum 中的无符号 int 值被视为有符号十进制整数 printf %i
Because unsigned int value that is stored in sum is treated like signed decimal integer in printf %i