从带符号整型的转换Conversions from Signed Integral Types

当一个带符号整数转换为一个具有相等或更大大小的无符号整数且该带符号整数的值不为负时,值保持不变。When a signed integer is converted to an unsigned integer with equal or greater size and the value of the signed integer is not negative, the value is unchanged. 转换是通过对带符号整数进行符号扩展来实现的。The conversion is made by sign-extending the signed integer. 通过截断高位将带符号整数转换为较短的带符号整数。A signed integer is converted to a shorter signed integer by truncating the high-order bits. 结果被解释为无符号值,如此示例中所示。The result is interpreted as an unsigned value, as shown in this example.

int i = -3;  
unsigned short u;  

u = i;   
printf_s( "%hu\n", u );  // Prints 65533  

将带符号整数转换为浮点值时,不会丢任何信息,只不过在将 long intunsigned long int 值转换为 float 值时,可能会丢失某些精度。No information is lost when a signed integer is converted to a floating value, except that some precision may be lost when a long int or unsigned long int value is converted to a float value.

下表汇总了来自带符号整型的转换。The following table summarizes conversions from signed integral types. 此表假定默认情况下 char 类型是带符号的。This table assumes that the char type is signed by default. 如果使用编译时选项将 char 类型的默认值更改为无符号,则应用 unsigned char 类型的从无符号整型转换表中给定的转换,而不是应用“从带符号整型转换”表中给定的转换。If you use a compile-time option to change the default for the char type to unsigned, the conversions given in the Conversions from Unsigned Integral Types table for the unsigned char type apply instead of the conversions in the following table, Conversions from Signed Integral Types.

从带符号整型的转换Conversions from Signed Integral Types

FromFrom To 方法Method
char1char1 shortshort 符号扩展Sign-extend
charchar longlong 符号扩展Sign-extend
charchar unsigned charunsigned char 保留模式;高序位失去符号位的函数Preserve pattern; high-order bit loses function as sign bit
charchar unsigned shortunsigned short 符号扩展至 short;将 short 转换为 unsigned shortSign-extend to short; convert short to unsigned short
charchar unsigned longunsigned long 符号扩展至 long;将 long 转换为 unsigned longSign-extend to long; convert long to unsigned long
charchar floatfloat 符号扩展至 long;将 long 转换为 floatSign-extend to long; convert long to float
charchar 双精度double 符号扩展至 long;将 long 转换为 doubleSign-extend to long; convert long to double
charchar long doublelong double 符号扩展至 long;将 long 转换为 doubleSign-extend to long; convert long to double
shortshort charchar 保留低位字节Preserve low-order byte
shortshort longlong 符号扩展Sign-extend
shortshort unsigned charunsigned char 保留低位字节Preserve low-order byte
shortshort unsigned shortunsigned short 保留位模式;高序位丢失符号位的函数Preserve bit pattern; high-order bit loses function as sign bit
shortshort unsigned longunsigned long 符号扩展至 long;将 long 转换为 unsigned longSign-extend to long; convert long to unsigned long
shortshort floatfloat 符号扩展至 long;将 long 转换为 floatSign-extend to long; convert long to float
shortshort 双精度double 符号扩展至 long;将 long 转换为 doubleSign-extend to long; convert long to double
shortshort long doublelong double 符号扩展至 long;将 long 转换为 doubleSign-extend to long; convert long to double
longlong charchar 保留低位字节Preserve low-order byte
longlong shortshort 保留低位字Preserve low-order word
longlong unsigned charunsigned char 保留低位字节Preserve low-order byte
longlong unsigned shortunsigned short 保留低位字Preserve low-order word
longlong unsigned longunsigned long 保留位模式;高序位丢失符号位的函数Preserve bit pattern; high-order bit loses function as sign bit
longlong floatfloat 表示为 floatRepresent as float. 如果不能精确表示 long,则某些精度将丢失。If long cannot be represented exactly, some precision is lost.
longlong 双精度double 表示为 doubleRepresent as double. 如果 long 不能精确表示为 double,则某些精度将丢失。If long cannot be represented exactly as a double, some precision is lost.
longlong long doublelong double 表示为 doubleRepresent as double. 如果 long 不能精确表示为 double,则某些精度将丢失。If long cannot be represented exactly as a double, some precision is lost.
  1. 所有 char 项假定默认情况下 char 类型是有符号的。All char entries assume that the char type is signed by default.

    Microsoft 专用Microsoft Specific

    对于 Microsoft 32 位 C 编译器,整数与 long 等效。For the Microsoft 32-bit C compiler, an integer is equivalent to a long. int 值的转换与 long 的相同。Conversion of an int value proceeds the same as for a long.

    结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

赋值转换Assignment Conversions