是无符号的字符总是提升为int? [英] Is unsigned char always promoted to int?
问题描述
假设如下:
unsigned char型富= 3;
unsigned char型吧= 5;unsigned int类型SHMOO = foo的酒吧+;
是富
和栏
保证晋升为 INT $ C值为前pression的评价$ C>值
富+酒吧
- 或者实现允许以促进它们无符号整型
?
在部分6.2.5 第8段:
对于任何两个整数类型具有相同的符号性和不同的整数转换等级
(见6.3.1.1),以较小的整数转换等级的类型值的范围是
其它类型的值的子范围。
块引用>在部分6.2.5 第9段:
如果一个
INT
可以重新present原始类型的所有值,值转换为INT
;否则,将其转换为unsigned int类型
块引用>这与较小的整数变换排名整数类型都有一个数值范围是其他类型的值的子范围的保证似乎依赖的符号性的整数类型。
符号字符
对应符号int
unsigned char型
对应unsigned int类型
这是否意味着一个
unsigned char型
的值只保证在unsigned int类型的子范围
并不见得INT
?如果是这样,这是否意味着一个实现理论上可以有一个unsigned char型
值是不是在一个子范围INT
?解决方案
都实现允许他们提升到unsigned int类型?
块引用>实现将促进
unsigned int类型
如果不是全部unsigned char型
值将在重新presentableINT
(如C99裁定由6.2.5p9)。请参见下面的实现示例。
如果是这样,这是否意味着一个实现理论上有unsigned char值,这是不是在一个int的子范围?
块引用>是的,例如:DSP CPU与
CHAR_BIT
16或32例如,TI C编译器的TMS320C55x:
CHAR_BIT
16和UCHAR_MAX
65535UINT_MAX
65535,但INT_MAX
32767<一个href=\"http://focus.ti.com/lit/ug/spru281f/spru281f.pdf\">http://focus.ti.com/lit/ug/spru281f/spru281f.pdf
Suppose the following:
unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar;
Are
foo
andbar
values guaranteed to be promoted toint
values for the evaluation of the expressionfoo + bar
-- or are implementations allowed to promote them tounsigned int
?In section 6.2.5 paragraph 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.
In section 6.2.5 paragraph 9:
If an
int
can represent all values of the original type, the value is converted to anint
; otherwise, it is converted to anunsigned 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
corresponds tosigned int
unsigned char
corresponds tounsigned int
Does this mean that the value of an
unsigned char
is only guaranteed to be in the subrange ofunsigned int
and not necessarilyint
? If so, does that imply that an implementation could theoretically have anunsigned char
value which is not in the subrange of anint
?解决方案are implementations allowed to promote them to unsigned int?
Implementations will promote to
unsigned int
if not allunsigned char
values are representable in anint
(as ruled by 6.2.5p9 in C99). See below for implementation examples.If so, does that imply that an implementation could theoretically have an unsigned char value which is not in the subrange of an int?
Yes, example: DSP cpu with
CHAR_BIT
16 or 32.For example, TI C compiler for TMS320C55x:
CHAR_BIT
is 16 andUCHAR_MAX
65535,UINT_MAX
65535 butINT_MAX
32767.http://focus.ti.com/lit/ug/spru281f/spru281f.pdf
这篇关于是无符号的字符总是提升为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!