为什么这个C程序输出负数?

时间:2022-06-04 01:42:02

I have assigned the complement value in an unsigned variable.

我在无符号变量中分配了补码值。

Then why this C program outputs a negative number?

那么为什么这个C程序输出一个负数?

#include<stdio.h>
#include<conio.h>

int main()
{
    unsigned int Value = 4;         /*   4 = 0000 0000  0000 0100 */  
    unsigned int result = 0;

    result = ~ Value;               /* -5 = 1111 1111  1111 1011 */  

    printf("result = %d", result);  /* -5             */

    getch();

    return 0;
}

2 个解决方案

#1


14  

The %d format specifier instructs printf to treat the argument as a signed integer. Use %u instead.

%d格式说明符指示printf将参数视为有符号整数。请改用%u。

#2


4  

It's because %d is the signed int format placeholder, so it's getting converted. Use %u for unsigned.

这是因为%d是带符号的int格式占位符,因此它被转换。使用%u表示未签名。

#1


14  

The %d format specifier instructs printf to treat the argument as a signed integer. Use %u instead.

%d格式说明符指示printf将参数视为有符号整数。请改用%u。

#2


4  

It's because %d is the signed int format placeholder, so it's getting converted. Use %u for unsigned.

这是因为%d是带符号的int格式占位符,因此它被转换。使用%u表示未签名。