将无效值赋给unsigned int时会发生什么? [重复]

时间:2022-07-05 16:07:56

Possible Duplicate:
signed to unsigned conversion in C - is it always safe?

可能重复:在C中签名为无符号转换 - 它总是安全吗?

Let's say I declare a variable of type unsigned int : unsigned int x = -1;

假设我声明了一个unsigned int类型的变量:unsigned int x = -1;

Now -1 in two's complement (assuming 32 bit machine) is 0xFFFFFFFF. Now when I assigned this value to x, did the value 0x7FFFFFFF get assigned to x?

现在-1的二进制补码(假设32位机器)是0xFFFFFFFF。现在,当我将此值赋给x时,值0x7FFFFFFF是否已分配给x?

If it were so, then printf ("%d",x); would have printed the decimal equivalent of 0x7FFFFFFF, right? But, clearly this isn't happening, as the value that gets printed is -1. What am I missing here?

如果是这样,则printf(“%d”,x);会打印十进制等效的0x7FFFFFFF,对吧?但是,显然这没有发生,因为打印的值是-1。我在这里想念的是什么?

Edit: I know that we can use the %u format specifier to print unsigned values. But that doesn't help answer the question above.

编辑:我知道我们可以使用%u格式说明符来打印无符号值。但这无助于回答上述问题。

3 个解决方案

#1


16  

The "%d" format is for (signed) int values. If you use it with an unsigned value, it could print something other than the actual value. Use "%u" to see the actual value, or %x to see it in hexadecimal.

“%d”格式用于(带符号)int值。如果使用无符号值,则可以打印除实际值以外的值。使用“%u”查看实际值,或使用%x以十六进制查看。

In the declaration

在宣言中

unsigned int x = -1;

the expression -1 is of type int, and has the value -1. The initializer converts this value from int to unsigned int. The rules for signed-to-unsigned conversion say that the value is reduced modulo UINT_MAX + 1, so -1 will convert to UINT_MAX (which is probably 0xffffffff or 4294967295 if unsigned int is 32 bits).

表达式-1的类型为int,其值为-1。初始值设定项将此值从int转换为unsigned int。有符号转换的规则表示该值以模UINT_MAX + 1减少,因此-1将转换为UINT_MAX(如果unsigned int为32位,则可能为0xffffffff或4294967295)。

You simply cannot assign a negative value to an object of an unsigned type. Any such value will be converted to the unsigned type before it's assigned, and the result will always be >= 0.

您根本无法将负值分配给无符号类型的对象。在分配之前,任何此类值都将转换为无符号类型,结果将始终> = 0。

#2


6  

Use %u instead of %d in order to print unsigned values. Then you should see 0xFFFFFFFF.

使用%u而不是%d来打印无符号值。然后你应该看到0xFFFFFFFF。

#3


0  

What is happening is that you convert the value first to unsigned int, assigning 0xffffffff to x. Then using printf("%d\n") you will convert the value back to signed int still keeping the value of 0xffffffff. Thus printing -1.

发生的事情是你首先将值转换为unsigned int,将0xffffffff赋值给x。然后使用printf(“%d \ n”),您将值转换回signed int,仍然保持0xffffffff的值。从而打印-1。

#1


16  

The "%d" format is for (signed) int values. If you use it with an unsigned value, it could print something other than the actual value. Use "%u" to see the actual value, or %x to see it in hexadecimal.

“%d”格式用于(带符号)int值。如果使用无符号值,则可以打印除实际值以外的值。使用“%u”查看实际值,或使用%x以十六进制查看。

In the declaration

在宣言中

unsigned int x = -1;

the expression -1 is of type int, and has the value -1. The initializer converts this value from int to unsigned int. The rules for signed-to-unsigned conversion say that the value is reduced modulo UINT_MAX + 1, so -1 will convert to UINT_MAX (which is probably 0xffffffff or 4294967295 if unsigned int is 32 bits).

表达式-1的类型为int,其值为-1。初始值设定项将此值从int转换为unsigned int。有符号转换的规则表示该值以模UINT_MAX + 1减少,因此-1将转换为UINT_MAX(如果unsigned int为32位,则可能为0xffffffff或4294967295)。

You simply cannot assign a negative value to an object of an unsigned type. Any such value will be converted to the unsigned type before it's assigned, and the result will always be >= 0.

您根本无法将负值分配给无符号类型的对象。在分配之前,任何此类值都将转换为无符号类型,结果将始终> = 0。

#2


6  

Use %u instead of %d in order to print unsigned values. Then you should see 0xFFFFFFFF.

使用%u而不是%d来打印无符号值。然后你应该看到0xFFFFFFFF。

#3


0  

What is happening is that you convert the value first to unsigned int, assigning 0xffffffff to x. Then using printf("%d\n") you will convert the value back to signed int still keeping the value of 0xffffffff. Thus printing -1.

发生的事情是你首先将值转换为unsigned int,将0xffffffff赋值给x。然后使用printf(“%d \ n”),您将值转换回signed int,仍然保持0xffffffff的值。从而打印-1。