字符会在C表达式中自动提升吗? [英] Are chars automatically promoted in C expressions?

查看:42
本文介绍了字符会在C表达式中自动提升吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向我的一位同事发了言:

I made a statement to a colleague of mine, which was:

字符会在C表达式中自动提升为整数,这对性能很好,因为CPU的自然字长最快.

"chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size.

我认为,由于字符的排名,字符提升行为在标准中有所说明.

I believe char promotion behavior is stated somewhere in the standard due to a char's rank.

这是我得到的答复:

字符未默认提升为整数.寄存器大小是32位,但可以将一行中的多个字节值打包到一个单寄存器作为编译器实现.这并不总是预测性的.您唯一可以验证自动升级的时间是当类型不包装在一个结构,因为C标准正式需要32位值调用堆栈内存.大量的CPU架构都进行了优化汇编要求使用非32位值,因此无法做任何假设关于这种情况下的CPU或编译器."

"Characters are not default promoted to an integer. The register size is 32 bit, but multiple byte values in a row can be packed into a single register as a compiler implementation. This is not always predictive. The only time you can verify automatic promotion is when the type is passed into the call stack when not wrapped around a structure because C standard officially needs 32-bit values in the call stack memory. A great deal of CPU architectures have optimized assembly calls for non-32 bit values, so no assumptions can be made about the CPU or compiler in this case."

我不确定谁是正确的,以及该相信什么.事实是什么?

I'm not sure who is right, and what to believe. What are the facts?

推荐答案

在C表达式中,字符自动升为整数

chars are automatically promoted to integers in C expressions

是的,他们是.C99第6.3.1.8节,常用算术转换:

Yes, they are. C99 section 6.3.1.8, Usual arithmetic conversions:

许多期望算术类型的操作数的运算符会导致转换并产生结果以类似的方式输入.目的是确定操作数的通用实型结果.对于指定的操作数,将转换每个操作数,而无需更改类型域,将其类型转换为对应的实型是普通实型.除非除非另有明确说明,否则普通实型也是对应的实型结果,如果操作数相同,则其类型域是操作数的类型域,否则就很复杂.这种模式称为通常的算术转换:

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

  • 首先,如果一个操作数的对应实型为long double,则另一个操作数将在不更改类型域的情况下转换为相应的实型类型为long double的类型.
  • 否则,如果任一操作数的对应实型为double,则另一个在不更改类型域的情况下,将操作数转换为其类型对应的实型是double.
  • 否则,如果两个操作数的对应实型为float,则另一个在不更改类型域的情况下,将操作数转换为其类型对应的实型是float.62)
  • 否则,将对两个操作数执行整数提升.然后以下规则适用于提升后的操作数:
    • 如果两个操作数具有相同的类型,则无需进一步转换.
    • 否则,如果两个操作数都具有符号整数类型或都具有无符号类型整数类型,具有较小整数转换等级的类型的操作数为转换为具有更高等级的操作数的类型.
    • 否则,如果具有无符号整数类型的操作数的秩更大或等于另一个操作数类型的等级,然后是有符号整数类型将转换为无符号操作数的类型整数类型.
    • 否则,如果带符号整数类型的操作数的类型可以表示具有无符号整数类型的操作数类型的所有值,然后无符号整数类型的操作数将转换为带符号整数类型的操作数.
    • 否则,两个操作数都将转换为无符号整数类型对应于带符号整数类型的操作数的类型.
    • First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.
    • Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
    • Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)
    • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
      • 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.

      整数升级在第6.3.1.1.2节中进行了描述:

      Integer promotions are described on Section 6.3.1.1.2:

      以下表达式可以在表达式中使用,无论是int还是unsigned可以使用int:

      The following may be used in an expression wherever an int or unsigned int may be used:

      • 具有整数类型的对象或表达式,其整数转换等级小于或等于int和unsigned的等级诠释.
      • _Bool,int,signed int或unsigned int类型的位字段

      如果一个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. These are called the integer promotions. All other types are unchanges by the integer promotions.

      char 的等级小于或等于 int 的等级,因此此处包含 char .

      The rank of a char is less than or equal to that of an int, so char is included in here.

      (作为一个脚注,提到整数提升仅作为常规算术转换的一部分,应用于某些参数表达式,一元 + -,以及移位运算符的两个操作数).

      (As a footnote, it is mentioned that integer promotions are only applied as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, - and ~, and to both operands of the shift operators).

      如评论中所述,整数提升还对函数调用参数执行.

      As mentioned in the comments, integer promotion is also performed on function-call arguments.

      这篇关于字符会在C表达式中自动提升吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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