gpt4 book ai didi

c++ - 字符是否在 C 表达式中自动提升?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:56 25 4
gpt4 key购买 nike

我对我的一位同事发表了声明,内容是:

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

我相信由于字符的等级,字符提升行为已在标准中的某处说明。

这是我得到的回复:

"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."

我不确定谁是对的,也不知道该相信什么。事实是什么?

最佳答案

chars are automatically promoted to integers in C expressions

是的,他们是。 C99 第 6.3.1.8 节,常规算术转换:

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:

  • 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 节中有描述:

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

  • An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned int.
  • A bit-field of type _Bool, int, signed int, or 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包含在这里。

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

如评论中所述,整数提升也会对函数调用参数执行。

关于c++ - 字符是否在 C 表达式中自动提升?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383507/

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