Conversions from Signed Integral Types

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

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  
  

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. This table assumes that the char type is signed by default. 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

FromToMethod
char1shortSign-extend
charlongSign-extend
charunsigned charPreserve pattern; high-order bit loses function as sign bit
charunsigned shortSign-extend to short; convert short to unsigned short
charunsigned longSign-extend to long; convert long to unsigned long
charfloatSign-extend to long; convert long to float
chardoubleSign-extend to long; convert long to double
charlong doubleSign-extend to long; convert long to double
shortcharPreserve low-order byte
shortlongSign-extend
shortunsigned charPreserve low-order byte
shortunsigned shortPreserve bit pattern; high-order bit loses function as sign bit
shortunsigned longSign-extend to long; convert long to unsigned long
shortfloatSign-extend to long; convert long to float
shortdoubleSign-extend to long; convert long to double
shortlong doubleSign-extend to long; convert long to double
longcharPreserve low-order byte
longshortPreserve low-order word
longunsigned charPreserve low-order byte
longunsigned shortPreserve low-order word
longunsigned longPreserve bit pattern; high-order bit loses function as sign bit
longfloatRepresent as float. If long cannot be represented exactly, some precision is lost.
longdoubleRepresent as double. If long cannot be represented exactly as a double, some precision is lost.
longlong doubleRepresent as double. If long cannot be represented exactly as a double, some precision is lost.
  1. All char entries assume that the char type is signed by default.

Microsoft Specific

For the Microsoft 32-bit C compiler, an integer is equivalent to a long. Conversion of an int value proceeds the same as for a long.

END Microsoft Specific

Assignment Conversions

Show: