c - 为什么 printf ("%d", ~a);当 a 等于 3 时显示 -4?

标签 c char printf unsigned negation

运行以下程序显示 -4 而预期为 252:

unsigned char a=3;
printf ("%d", ~a);

为什么这段代码不显示252?

我还根据建议的答案测试了以下内容:

printf ("%u", ~a);

显示:4294967292

printf ("%hu", ~a);

显示:65532

为什么 ~a 不返回 unsigned char 因为 a unsigned char

我的问题不是我应该怎么做才能显示 252 ?我的问题是为什么不显示252?

最佳答案

除了the answer of @Someprogrammerdude , 以下是 The Book1) 中的相关段落:

Unary arithmetic operators (§6.5.3.3/4)

The result of the ~ operator is the bitwise complement of its (promoted [ !! ] ) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.

Arithmetic operands - Boolean, characters, and integers (§6.3.1.1) :

  1. Every integer type has an integer conversion rank defined as follows:

    • No two signed integer types shall have the same rank, even if they have the same representation.
    • The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
    • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
    • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
    • The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
    • The rank of char shall equal the rank of signed char and unsigned char.
    • The rank of _Bool shall be less than the rank of all other standard integer types.
    • The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2).
    • The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined, but still subject to the other rules for determining the integer conversion rank.
    • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.
  2. The following may be used in an expression wherever an int or unsigned int may be used:

    • An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned int.
    • A bit-field of type _Bool, int, signed int, or unsigned int. If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.
  3. The integer promotions preserve value including sign. As discussed earlier, whether a "plain" char is treated as signed is implementation-defined.

48) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.


您的问题:

Why ~a doesn't return an unsigned char since a is an unsigned char?

因为适用整数促销。

unsigned char a = 3;
printf ("%d", ~a);

a 是一个 unsigned char,一种范围可以用 int 表示的类型。所以 a 被提升为 int。假设 32 位宽 inttwo's complement :

310 = 0000 0000 0000 0000 0000 0000 0000 00112
~310 = 1111 1111 1111 1111 1111 1111 1111 11002

解释为 signed int 的结果是负数,因为设置了最高有效位,即符号位。

转换为十进制:

1111 1111 1111 1111 1111 1111 1111 11002
¬ 0000 0000 0000 0000 0000 0000 0000 00112
+ 0000 0000 0000 0000 0000 0000 0000 00012
──────────────────────────────
0000 0000 0000 0000 0000 0000 0000 01002

01002 = 0 × 23 + 1 × 22 + 0 × 22 + 0 × 22
= 1 × 22
= 410
= −410(带原符号)

~> printf() 打印 -4

要使用使用 "%d" 作为格式说明符的原始代码获得 252 的预期结果,需要进行一些转换:

unsigned char a = 3;
printf("%d\n", (int)((unsigned char) ~a));  // prints 252
//              ^^^   ^^^^^^^^^^^^^
//               |          cast the result of ~a back to unsigned char *)
//               |          to discard the bits > CHAR_BIT
//               cast the char back to int to agree with the format specifier 

*)感谢 chux 让我记住 char 可以被签名!强制转换为(可能是 signed)char 会给出 -4 的错误结果。

要在不进行转换的情况下获得相同的结果,您可以使用长度修饰符 hh:

The fprintf function (§7.19.6.1/7)

The length modifiers and their meanings are:

hh  Specifies that a following d, i, o, u, x, or X conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing); or that a following n conversion specifier applies to a pointer to a signed char argument.

[...]

unsigned char a = 3;
printf("%hhu\n", ~a);  // prints 252


你其他尝试的问题:

printf ("%u", ~a);

displays: 4294967292

printf ("%hu", ~a);

displays: 65532

由于 ~a 是一个 integer,它不是格式说明符 u

的正确类型

The fprintf function (§7.19.6.1/9) :

If a conversion specification is invalid, the behavior is undefined.248) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.



1) ISO/IEC 9899/Cor3:2007 又名 C99:TR3 又名 C99

关于c - 为什么 printf ("%d", ~a);当 a 等于 3 时显示 -4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53479444/

相关文章:

php - .sprintf ("%4.2f", value) 如何在 PHP 中运行?

java - 我无法让我的 printf 语句在 java 中对齐

c - 如何声明一个函数,该函数的结构包含一个数组,其大小由 argv 确定,作为参数?

c - 如何在C语言编程中禁用覆盖函数

c - 如何从 char* 函数返回 char*

c++ - std::cin >> *aa 导致总线错误

C:数组初始化段错误取决于大小和对 printf() 的调用

c - 为什么 GCC 在实现整数除法时使用乘以一个奇怪的数字?

将整数转换为 0x 等价物(十六进制)

c++ - 多字符字符常量变量的值是多少