C 中的无符号值 [英] Unsigned values in C

查看:66
本文介绍了C 中的无符号值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

#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;
}

输出为:

ffffffff
ffffffff
-1
-1
4294967295
4294967295

我可以看到根据传递给 printf 函数的值,一个值被解释为有符号或无符号.在这两种情况下,字节是相同的 (ffffffff).那么,unsigned 这个词是什么意思?

I can see that a value is interpreted as signed or unsigned according to the value passed to printf function. In both cases, the bytes are the same (ffffffff). Then, what is the unsigned word for?

推荐答案

Assign a int -1 to an unsigned: As -1不适合 [0...UINT_MAX] 范围,添加 UINT_MAX+1 的倍数,直到答案在范围内.显然 UINT_MAX 在 OP 的机器上是 pow(2,32)-1 或 429496725 所以 a 的值是4294967295.

Assign a int -1 to an unsigned: As -1 does not fit in the range [0...UINT_MAX], multiples of UINT_MAX+1 are added until the answer is in range. Evidently UINT_MAX is pow(2,32)-1 or 429496725 on OP's machine so a has the value of 4294967295.

unsigned int a = -1;

"%x", "%u" 说明符需要匹配的 unsigned.由于这些不匹配,如果转换规范无效,则行为未定义.如果任何参数不是相应转换规范的正确类型,则行为未定义."C11 §7.21.6.1 9. printf 说明符不会改变 b.

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.

printf("%x\n", b);  // UB
printf("%u\n", b);  // UB

"%d" 说明符需要一个匹配的 int.由于这些不匹配,更多UB.

The "%d" specifier expects a matching int. Since these do not match, more UB.

printf("%d\n", a);  // UB

鉴于未定义的行为,不支持结论.

Given undefined behavior, the conclusions are not supported.

两种情况,字节相同(ffffffff).

both cases, the bytes are the same (ffffffff).

即使位模式相同,不同的类型也可能有不同的值.ffffffff 作为 unsigned 的值为 4294967295.作为 int,取决于有符号整数编码,它的值为 -1, -2147483647或待定.作为 float,它可能是 NAN.

Even with the same bit pattern, different types may have different values. ffffffff as an unsigned has the value of 4294967295. As an int, depending signed integer encoding, it has the value of -1, -2147483647 or TBD. As a float it may be a NAN.

什么是无符号词?

unsigned 存储 [0 ... UINT_MAX] 范围内的整数.它永远不会有负值.如果代码需要一个非负数,请使用 unsigned.如果代码需要一个可能是 +、- 或 0 的计数数字,请使用 int.

unsigned stores 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.

更新:为了避免编译器警告将有符号 int 分配给 unsigned,请使用以下内容.这是一个被否定的 unsigned 1u - 如上所述.效果与 -1 相同,但传达给编译器的直接意图.

Update: to avoid a compiler warning about assigning a signed int to unsigned, use the below. This is an unsigned 1u being negated - which is well defined as above. The effect is the same as a -1, but conveys to the compiler direct intentions.

unsigned int a = -1u;

这篇关于C 中的无符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆