C语言的隐式类型转换的规则——从第一手信源(C的标准)入手

一:C89

原文:

3.2 CONVERSIONS

   Several operators convert operand values from one type to another automatically.  This section specifies the result required from such an implicit conversion, as well as those that result from a cast operation (an explicit conversion).  The list in $3.2.1.5 summarizes the conversions performed by most ordinary operators; it is supplemented as required by the discussion of each operator in $3.3.   Conversion of an operand value to a compatible type causes no change. 

Forward references: cast operators ($3.3.4). 

3.2.1 Arithmetic operands

3.2.1.1 Characters and integers

   A char, a short int, or an int bit-field, or their signed or unsigned varieties, or an object that has enumeration type, may be used in an expression wherever an int or unsigned int may be used.  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 integral promotions.

解读:

1、char、unsigned char、short、unsigned short、int转换为int

2、unsigned int 转换为unsigned int(不变)

以上规则称为整形提升(integral promotions)

3.2.1.5 Usual arithmetic conversions

   Many binary operators that expect operands of arithmetic type cause conversions and yield result types in a similar way.  The purpose is to yield a common type, which is also the type of the result.  This pattern is called the usual arithmetic conversions: First, if either operand has type long double, the other operand is converted to long double .  Otherwise, if either operand has type double, the other operand is converted to double.  Otherwise, if either operand has type float, the other operand is converted to float.  Otherwise, the integral promotions are performed on both operands.  Then the following rules are applied: If either operand has type unsigned long int, the other operand is converted to unsigned long int. Otherwise, if one operand has type long int and the other has type unsigned int, if a long int can represent all values of an unsigned int, the operand of type unsigned int is converted to long int ; if a long int cannot represent all the values of an unsigned int, both operands are converted to unsigned long int.  Otherwise, if either operand has type long int, the other operand is converted to long int. Otherwise, if either operand has type unsigned int, the other operand is converted to unsigned int.  Otherwise, both operands have type int.

   The values of operands and of the results of expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

解读:

1:如果一个操作数的类型为long double,则另一个操作数将转换为long double。

2:否则,如果其中一个操作数的类型为double,则另一个操作数将转换为double。

3:否则,如果其中一个操作数的类型为float,则另一个操作数将转换为float。

4:否则,将对两个操作数执行整数提升。然后应用以下规则:

      4.1如果任一操作数的类型为unsigned long int,则另一个操作数将转换为unsigned long int。

      4.2 否则,如果一个操作数的类型为long int,而另一个操作数的类型为unsigned int.

              4.2.1如果long int可以表示unsigned的所有值int,将unsigned int类型的操作数转换为long int;

             4.2.2如果一个long int不能代表unsigned int的所有值,两者操作数将转换为unsigned long int。

      4.3 否则,如果有操作数的类型为long int,另一个操作数将转换为long int。

      4.4否则,如果其中一个操作数的类型为unsigned int,则另一个操作数将转换为unsigned int。

      4.5否则,两个操作数都具有int类型。

一个栗子:

unsigned int a = 10;

int b = -20;

if(b < a) printf(“b<a\n”);

else printf(“b>a\n”);

以上代码输出b>a。解释:隐式转换流程转到4.4

 

二:C99标准

原文:

6.3 Conversions

1 Several operators convert operand values from one type to another automatically. This subclause specifies the result required from such an implicit conversion, as well as those that result from a cast operation (an explicit conversion). The list in 6.3.1.8 summarizes the conversions performed by most ordinary operators; it is supplemented as required by the discussion of each operator in 6.5.

2 Conversion of an operand value to a compatible type causes no change to the value or the representation.

Forward references: cast operators (6.5.4).

6.3.1 Arithmetic operands

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 hav e 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.

解读:

1: 秩:long long int> long int>int>short>char

2、任何无符号整形和相应的有符号整型的秩相同,即unsigned int和int的秩相同

 

— 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, ifT1 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 whose integer conversion rank is less than the rank of int and unsigned int.

— A bit-field of type _Bool, int, signed int, orunsigned 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.48) All other types are unchanged by the integer promotions.

解读:

1、char、unsigned char、short、unsigned short、int转换为int

2、unsigned int 转换为unsigned int(不变)

以上规则称为整形提升(integral promotions)

以上规则同C89

 

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

Forward references: enumeration specifiers (6.7.2.2), structure and union specifiers (6.7.2.1).

 

……

 

6.3.1.8 Usual arithmetic conversions

1 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.51)

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.

解读:

1:如果一个操作数的类型为long double,则另一个操作数将转换为long double。

2:否则,如果其中一个操作数的类型为double,则另一个操作数将转换为double。

3:否则,如果其中一个操作数的类型为float,则另一个操作数将转换为float。

4:否则,将对两个操作数执行整数提升。然后应用以下规则:

       4.1如果两个操作数具有相同的类型,则无需进一步转换。

       4.2否则,如果两个操作数都是有符号整数类型或都是无符号整数类型,则将具有较小秩的操作数转换为具有较大秩的操作数的类型。

       4.3否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则带符号整数类型的操作数将转换为无符号整数类型的操作数的类型。

      4.4否则,如果带符号整数类型的操作数的类型可以表示无符号整数类型的操作数的所有值,则带无符号整数类型的操作数将转换为带符号整数类型的操作数的类型。

      4.5否则,两个操作数都将转换为与带符号整数类型的操作数类型相对应的无符号整数类型。

 

C语言标准参考:

http://www.iso.org/iso/catalogue_detail.htm?csnumber=29237

http://www.open-std.org/jtc1/sc22/wg14/

http://nepsweb.co.uk/langstand/isoC/index.htm

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值