gpt4 book ai didi

c - unsigned char 总是提升为 int 吗?

转载 作者:太空狗 更新时间:2023-10-29 17:21:27 24 4
gpt4 key购买 nike

假设如下:

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

unsigned int shmoo = foo + bar;

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

第 6.2.5 节第 8 段中:

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 段中:

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.

保证具有较小整数转换等级的整数类型的值范围是另一种类型值的子范围似乎取决于整数类型的符号

  • signed char对应signed int
  • unsigned char对应unsigned int

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

最佳答案

are implementations allowed to promote them to unsigned int?

如果不是所有的 unsigned char 值都可以用 int 表示(根据 C99 中的 6.2.5p9 规定),实现将提升为 unsigned 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。

例如,用于 TMS320C55x 的 TI C 编译器:CHAR_BIT 为 16 和 UCHAR_MAX 65535,UINT_MAX 65535 但 INT_MAX 32767.

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

关于c - unsigned char 总是提升为 int 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com