Next: Boolean Type, Previous: Narrow Integers, Up: Integer Types [Contents][Index]
C converts between integer types implicitly in many situations. It
converts the narrow integer types, char
and short
, to
int
whenever they are used in arithmetic. Assigning a new
value to an integer variable (or other lvalue) converts the value to
the variable’s type.
You can also convert one integer type to another explicitly with a cast operator. See Explicit Type Conversion.
The process of conversion to a wider type is straightforward: the value is unchanged. The only exception is when converting a negative value (in a signed type, obviously) to a wider unsigned type. In that case, the result is a positive value with the same bits (see Integers in Depth).
Converting to a narrower type, also called truncation, involves discarding some of the value’s bits. This is not considered overflow (see Integer Overflow) because loss of significant bits is a normal consequence of truncation. Likewise for conversion between signed and unsigned types of the same width.
More information about conversion for assignment is in Assignment Type Conversions. For conversion for arithmetic, see Argument Promotions.