在存在 unsigned int 和signed int 的 C 表达式中,哪种类型将提升为哪种类型?

发布于 2024-08-22 01:40:01 字数 362 浏览 5 评论 0原文

我对C语言标准中的数据类型提升规则有疑问。 C99 规定:

C 整数提升还要求“如果 int 可以表示原始类型的所有值,则将该值转换为 int;否则,将其转换为无符号 int。”

我的问题是,如果存在 unsigned intsigned int 的 C 语言表达式,哪种类型将提升为哪种类型?

例如,int 无法表示 unsigned int 的所有值(大于 MAX_INT 值的值),而 unsigned int 则不能代表 -ve 值,那么在这种情况下什么类型会提升为什么?

I have a query about data type promotion rules in C language standard.
The C99 says that:

C integer promotions also require that "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."

My questions is in case of a C language expression where unsigned int and signed int are present, which type will be promoted to what type?

E.g. int cannot represent all the values of the unsigned int (values larger than MAX_INT values) whereas unsigned int cannot represent the -ve values, so what type is promoted to what in such cases?

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

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

发布评论

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

评论(2

无人问我粥可暖 2024-08-29 01:40:01

我认为你混淆了两件事。 提升是将比 int/unsigned int “小的”整数类型的值转换为 int 或 unsigned int 的过程。这些规则的表达有些奇怪(主要是为了充分处理字符),但确保值和符号得到保留。

然后是普通算术转换的不同概念,通过它可以将算术运算符的操作数转换为公共类型。首先,如果操作数的类型小于 int,则将其提升为 int 或 unsigned,然后通过以下过程选择目标类型(对于整数类型,6.3.1.8/1)

如果两个操作数具有相同的类型,则不需要进一步转换。

否则,如果两个操作数都具有有符号整数类型或都具有无符号整数类型
整数类型,整数转换等级较小的类型的操作数为
转换为具有更高等级的操作数类型。

否则,如果具有无符号整数类型的操作数的等级更大或
等于另一个操作数的类型的等级,然后操作数
有符号整数类型转换为无符号操作数类型
整数类型。

否则,如果有符号整型操作数的类型可以表示
操作数类型的所有值均为无符号整数类型,则
无符号整数类型的操作数被转换为
有符号整数类型的操作数。

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

(请注意 ISTR,这些规则在 C89 和 C99 之间略有变化)

I think you are confusing two things. Promotion is the process by which values of integer type "smaller" that int/unsigned int are converted either to int or unsigned int. The rules are expressed somewhat strangely (mostly for the benefit of handling adequately char) but ensure that value and sign are conserved.

Then there is the different concept of usual arithmetic conversion by which operands of arithmetic operators are converted to a common type. It begins by promoting the operand (to either int or unsigned) if they are of a type smaller than int and then choosing a target type by the following process (for integer types, 6.3.1.8/1)

If both operands have the same type, then no further conversion is needed.

Otherwise, if both operands have signed integer types or both have unsigned
integer types, the operand with the type of lesser integer conversion rank is
converted to the type of the operand with greater rank.

Otherwise, if the operand that has unsigned integer type has rank greater or
equal to the rank of the type of the other operand, then the operand with
signed integer type is converted to the type of the operand with unsigned
integer type.

Otherwise, if the type of the operand with signed integer type can represent
all of the values of the type of the operand with unsigned integer type, then
the operand with unsigned integer type is converted to the type of the
operand with signed integer type.

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

(Note that ISTR that those rules have changed slightly between C89 and C99)

や三分注定 2024-08-29 01:40:01

我认为以下内容可以回答您的问题:

6.3.1.3 有符号和无符号整数

1 当整数类型的值是
转换为另一种整数类型
除了 _Bool 之外,如果该值可以是
用新的类型来表示,就是
不变。

2 否则,如果新
类型是无符号的,值为
通过重复添加或转换
比最大值减一
可以表示的值
新类型,直到该值位于
新类型的范围。

3 否则,
新类型已签名且值
不能在其中体现;任何一个
结果是实现定义的
或者实现定义的信号是
提出。

I think the following answers your question:

6.3.1.3 Signed and unsigned integers

1 When a value with integer type is
converted to another integer type
other than _Bool, if the value can be
represented by the new type, it is
unchanged.

2 Otherwise, if the new
type is unsigned, the value is
converted by repeatedly adding or
subtracting one more than the maximum
value that can be represented in the
new type until the value is in the
range of the new type.

3 Otherwise,
the new type is signed and the value
cannot be represented in it; either
the result is implementation-defined
or an implementation-defined signal is
raised.

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