一千萬個為什麽

搜索

在c編譯器中輸入類型

請考慮以下示例支持混合模式表達式: c = a + b * 10.5 其中 ab 是整數, c 是一個浮點數。 描述數據如何被類型轉換為適當的格式

最佳答案

您需要在教科書或C標準(C11-ISO/IEC 9899:2011中的§6.3.1.8)中查找“常規算術轉換”,其內容如下:

6.3.1.8通常的算術轉換

     

1許多期望算術類型操作數的運算符會導致轉換並產生結果   以類似的方式輸入類型。目的是確定操作數的通用實數類型   和結果。對於指定的操作數,將轉換每個操作數,而不更改類型   域,對應的實際類型是通用實型的類型。除非   另外明確說明,常見的真實類型也是相應的實際類型   結果,其類型域是操作數的類型域,如果它們是相同的,   否則復雜。這種模式稱為通常的算術轉換:

     

首先,如果任一操作數的相應實數類型是 long double ,則另一個   操作數在不更改類型域的情況下轉換為對應的實類型為 long double 的類型。

     

否則,如果任一操作數的相應實數類型是 double ,則另一個   操作數在不更改類型域的情況下轉換為其類型的類型   對應的實際類型是 double

     

否則,如果任一操作數的相應實數類型是 float ,則另一個   操作數在不更改類型域的情況下轉換為其類型的類型   相應的實數類型是 float62)

     

否則,將對兩個操作數執行整數提升。然後   以下規則適用於提升的操作數:

     

如果兩個操作數具有相同的類型,則不需要進一步轉換。

     

否則,如果兩個操作數都有有符號整數類型或兩者都有無符號   整數類型,具有較小整數轉換等級類型的操作數是   轉換為具有更高等級的操作數的類型。

     

否則,如果具有無符號整數類型的操作數的等級大於或等於   等於另一個操作數的類型的等級,然後是操作數   有符號整數類型轉換為帶有unsigned的操作數的類型   整數類型。

     

否則,如果帶有符號整數類型的操作數的類型可以表示   那麽,帶有無符號整數類型的操作數類型的所有值   具有無符號整數類型的操作數將轉換為該類型   帶有符號整數類型的操作數。

     

否則,兩個操作數都將轉換為無符號整數類型   對應於帶有符號整數類型的操作數的類型。

     

2浮動操作數的值和浮動表達式的結果可能是   代表的範圍和精度高於該類型所要求的範圍和精度;類型不是   由此改變。 63)

     

62)例如,添加 double _Complexfloat 只需要轉換    float 操作數到 double (並產生 double _Complex 結果)。

     

63)仍然需要使用強制轉換和賦值運算符來消除額外的範圍和精度。

整數提升在§6.3.1.1中定義:

6.3.1.1 Boolean, characters, and integers

1 Every integer type has an integer conversion rank defined as follows:

  • No two signed integer types shall have the same rank, even if they have the same representation.
  • The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
  • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
  • The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
  • The rank of char shall equal the rank of signed char and unsigned char.
  • The rank of _Bool shall be less than the rank of all other standard integer types.
  • The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2).
  • The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined, but still subject to the other rules for determining the integer conversion rank.
  • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.

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 (other than int or unsigned int) 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 (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.58) All other types are unchanged by the integer promotions.

3 The integer promotions preserve value including sign. As discussed earlier, whether a ‘‘plain’’ char is treated as signed is implementation-defined.

58) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

轉載註明原文: 在c編譯器中輸入類型