在 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?
发布评论
评论(2)
回答你的第一个问题:不。由于
int32_t
通常是用这样的 typedef 定义的,因此它与
int
相同,并且与int
具有相同的等级。回答第二个问题:是的。整数提升仍然适用。 stdint.h 中定义的类型的行为就像它们的别名类型一样。
顺便说一句,为了对编译器的行为更有信心,您可以通过编写这样的无效代码并仔细查看错误消息来测试编译器中的所有这些内容,这将(如果您有一个好的编译器)揭示右侧表达式的类型:
To answer your first question: no. Since
int32_t
is usually defined with a typedef like thisit is the same as
int
and will have the same rank asint
.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:
根据C标准
2 的补码表示的精确整数类型被定义为标准整数类型的 tyoedef 别名。
来自 C 标准(7.20.1.1 精确宽度整数类型)
因此,当 int 类型有 32 位(用于 2 的补码表示)时,该关系
是正确的,除了关系
int > > 。 int32_t
前提是类型int32_t
是由 typedef 声明引入的类型int
的别名。这里,
unsigned char
类型的对象被提升为int
类型,uint32_t
类型的对象被提升为类型 整数提升,unsigned int
(假设int
为 32 位)由于C 标准的
然后,由于通常的算术转换,
int
类型的对象被转换为unsigned int
类型。来自 C 标准(6.3.1.8 常用算术转换)
请注意,名称
uint32_t
可以是 typedef 声明引入的类型unsigned int
的别名。在本例中,uint32_t
与unsigned int
的类型相同。According to the C Standard
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)
So this relational when the type int has 32 bits (for 2's complement representation)
is correct except that the relation
int > int32_t
provided that the typeint32_t
is an alias name for the typeint
introduced by a typedef declaration..Here the object of the type
unsigned char
is promoted to the typeint
and the object of the typeuint32_t
is promoted to the typeunsigned int
(provided thatint
has 32-bits) due to the integer promotionsFrom the C Standard
And then the object of the type
int
is converted to the typeunsigned int
due to the usual arithmetic conversions.From the C Standard (6.3.1.8 Usual arithmetic conversions)
Pay attention to then the name
uint32_t
can be an alias for the typeunsigned int
introduced by a typedef declaration. In this caseuint32_t
is the same type asunsigned int
.