stdint.h 的类型转换和整数提升如何工作?

发布于 2025-01-18 10:24:29 字数 353 浏览 2 评论 0 原文

在 C 中,我了解标准类型的类型转换、整数提升、强制转换等,但是 stdint.h 类型如何影响这一点?

对于类型排名,规则规定:

  • 任何两个有符号整数类型都不应具有相同的排名,即使它们具有相同的表示形式。
  • 任何无符号整数类型的等级应等于相应的有符号整数类型(如果有)的等级。

所以假设 int 是 32 位,这是否意味着 int > > int32_t = uint32_t >;排名中的short int

另外, stdint.h 类型是否也受到整数提升的影响?例如,如果我尝试将有符号字符添加到 uint32_t,它们都会提升为无符号整数吗?

In C, I understand type conversions, integer promotion, casting, etc. for standard types, but how do the stdint.h types factor into this?

For type rankings, the rules state:

  • No two signed integer types shall have the same rank, even if they have the same representation.
  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.

So assuming an int is 32 bits, does this mean int > int32_t = uint32_t > short int in the rankings?

Also, are the stdint.h types also subject to integer promotion? For example if I try to add a signed char to a uint32_t, they will both get promoted to unsigned ints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2025-01-25 10:24:29

回答你的第一个问题:不。由于 int32_t 通常是用这样的 typedef 定义的,因此

typedef int int32_t;

它与 int 相同,并且与 int 具有相同的等级。

回答第二个问题:是的。整数提升仍然适用。 stdint.h 中定义的类型的行为就像它们的别名类型一样。

顺便说一句,为了对编译器的行为更有信心,您可以通过编写这样的无效代码并仔细查看错误消息来测试编译器中的所有这些内容,这将(如果您有一个好的编译器)揭示右侧表达式的类型:

void * x = (signed char)-1 + (uint32_t)0;

To answer your first question: no. Since int32_t is usually defined with a typedef like this

typedef int int32_t;

it is the same as int and will have the same rank as int.

To answer the second question: yes. Integer promotion still applies. The types defined in stdint.h behave just like the types they are aliases of.

By the way, to be more confident in how your compiler behaves, you can test all of these things in your compiler by writing invalid code like this and carefully looking at the error message, which will (if you have a good compiler) reveal the type of the expression on the right hand side:

void * x = (signed char)-1 + (uint32_t)0;
盛装女皇 2025-01-25 10:24:29

根据C标准

——任何标准整数类型的等级应大于等级
具有相同宽度的任何扩展整数类型。

2 的补码表示的精确整数类型被定义为标准整数类型的 tyoedef 别名。

来自 C 标准(7.20.1.1 精确宽度整数类型)

  • ...and(对于有符号类型)具有二进制补码
    表示,它应定义相应的 typedef 名称。
  • 因此,当 int 类型有 32 位(用于 2 的补码表示)时,该关系

    int > int32_t = uint32_t > short int
    

    是正确的,除了关系 int > > 。 int32_t 前提是类型 int32_t 是由 typedef 声明引入的类型 int 的别名。

    此外,stdint.h 类型是否也受到整数提升的影响?为了
    例如,如果我尝试将签名字符添加到 uint32_t,它们都会
    晋升为无符号整数?

    这里,unsigned char 类型的对象被提升为 int 类型,uint32_t 类型的对象被提升为 类型 整数提升,unsigned int(假设 int 为 32 位)

    由于C 标准的

    如果一个 int 可以表示原始类型的所有值(受限制)
    通过宽度(对于位字段),将该值转换为 int;
    否则,它被转换为无符号整型。这些被称为
    整数促销。 58) 所有其他类型均不受整数影响
    促销活动。

    然后,由于通常的算术转换,int 类型的对象被转换为 unsigned int 类型。

    来自 C 标准(6.3.1.8 常用算术转换)

    否则,两个操作数都转换为无符号整数类型
    对应有符号整型操作数的类型。

    请注意,名称 uint32_t 可以是 typedef 声明引入的类型 unsigned int 的别名。在本例中,uint32_tunsigned int 的类型相同。

    According to the C Standard

    — The rank of any standard integer type shall be greater than the rank
    of any extended integer type with the same width.

    The exact integer types for 2's complement representation are defined as tyoedef aliases of standard integer types.

    From the C Standard (7.20.1.1 Exact-width integer types)

    1. ...and (for the signed types) that have a two’s complement
      representation, it shall define the corresponding typedef names.

    So this relational when the type int has 32 bits (for 2's complement representation)

    int > int32_t = uint32_t > short int
    

    is correct except that the relation int > int32_t provided that the type int32_t is an alias name for the type int introduced by a typedef declaration..

    Also, are the stdint.h types also subject to integer promotion? For
    example if I try to add a signed char to a uint32_t, they will both
    get promoted to unsigned ints?

    Here the object of the type unsigned char is promoted to the type int and the object of the type uint32_t is promoted to the type unsigned int (provided that int has 32-bits) due to the integer promotions

    From the C Standard

    If an int can represent all values of the original type (as restricted
    by the width, for a bit-field), the value is converted to an int;
    otherwise, it is converted to an unsigned int. These are called the
    integer promotions. 58) All other types are unchanged by the integer
    promotions.

    And then the object of the type int is converted to the type unsigned int due to the usual arithmetic conversions.

    From the C Standard (6.3.1.8 Usual arithmetic conversions)

    Otherwise, both operands are converted to the unsigned integer type
    corresponding to the type of the operand with signed integer type.

    Pay attention to then the name uint32_t can be an alias for the type unsigned int introduced by a typedef declaration. In this case uint32_t is the same type as unsigned int.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文