是无符号的字符总是提升为int? [英] Is unsigned char always promoted to int?

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

问题描述

假设如下:

  unsigned char型富= 3;
unsigned char型吧= 5;unsigned int类型SHMOO = foo的酒吧+;

保证晋升为 INT 富+酒吧 - 或者实现允许以促进它们无符号整型

部分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型值将在重新presentable INT (如C99裁定由6.2.5p9)。请参见下面的实现示例。


  

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


是的,例如:DSP CPU与 CHAR_BIT 16或32

例如,TI C编译器的TMS320C55x: CHAR_BIT 16和 UCHAR_MAX 65535 UINT_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 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?

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 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 corresponds to signed int
  • unsigned char corresponds to unsigned int

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?

解决方案

are implementations allowed to promote them to 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.

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 and UCHAR_MAX 65535, UINT_MAX 65535 but INT_MAX 32767.

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

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

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