unsigned char 是否总是提升为 int? [英] Is unsigned char always promoted to int?

查看:41
本文介绍了unsigned char 是否总是提升为 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设如下:

unsigned char foo = 3;
unsigned char bar = 5;

unsigned int shmoo = foo + bar;

foobar 值是否保证被提升为 int 值以评估表达式 foo + bar -- 或者是否允许实现将它们提升为 unsigned int?

Are foo and bar values guaranteed to be promoted to int values for the evaluation of the expression foo + bar -- or are implementations allowed to promote them to unsigned int?

第 6.2.5 节第 8 段:

对于任意两个具有相同符号和不同整数转换等级的整数类型(见 6.3.1.1),整数转换等级较小的类型的取值范围是其他类型值的子范围.

For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

第 6.2.5 节第 9 段:

如果一个 int 可以表示原始类型的所有值,则将该值转换为 int;否则,将其转换为 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.

保证具有较小整数转换等级的整数类型的值范围是其他类型值的子范围,这似乎取决于整数类型的有符号性.

The guarantee that an integer type with smaller integer conversion rank has a range of values that is a subrange of the values of the other type seems dependent on the signedness of the integer type.

  • signed char 对应于signed int
  • unsigned char 对应于unsigned int
  • signed char corresponds to signed int
  • unsigned char corresponds to unsigned int

这是否意味着 unsigned char 的值只能保证在 unsigned int 的子范围内,而不一定是 int?如果是这样,这是否意味着一个实现理论上可以有一个不在 int 子范围内的 unsigned char 值?

Does this mean that the value of an unsigned char is only guaranteed to be in the subrange of unsigned int and not necessarily int? If so, does that imply that an implementation could theoretically have an unsigned char value which is not in the subrange of an int?

推荐答案

是否允许实现将它们提升为 unsigned int?

are implementations allowed to promote them to unsigned int?

如果不是所有 unsigned char 值都可以在 int 中表示(由 C99 中的 6.2.5p9 规定),则实现将提升为 unsigned int).请参阅下面的实现示例.

Implementations will promote to unsigned int if not all unsigned char values are representable in an int (as ruled by 6.2.5p9 in C99). See below for implementation examples.

如果是这样,这是否意味着一个实现理论上可以有一个 unsigned char 值,它不在 int 的子范围内?

If so, does that imply that an implementation could theoretically have an unsigned char value which is not in the subrange of an int?

是的,例如:带有 CHAR_BIT 16 或 32 的 DSP cpu.

Yes, example: DSP cpu with CHAR_BIT 16 or 32.

例如,用于 TMS320C55x 的 TI C 编译器:CHAR_BIT is 16 and UCHAR_MAX 65535, UINT_MAX 65535 but INT_MAX 32767.

For example, TI C compiler for TMS320C55x: CHAR_BIT is 16 and UCHAR_MAX 65535, UINT_MAX 65535 but INT_MAX 32767.

http://focus.ti.com/lit/ug/spru281f/spru281f.pdf

这篇关于unsigned char 是否总是提升为 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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