Conversions
Conversions
In traditional C, specifying the -float option coerces floating point constants into
type float if all the other subexpressions are of type float. This is not the case in ANSI
C. ANSI C considers all floating point constants to be implicitly double precision, and
operations involving such constants therefore take place in double precision. To force
single precision arithmetic in ANSI C, use the f or F suffix on floating point constants.
To force long double precision on constants, use the l or L suffix. For
example, 3.14l is long double precision, 3.14 is double precision, and 3.14f is single
precision in ANSI C.
For a complete discussion with examples, see “Type Promotion and Floating Point
Constants”.
When a floating value is converted to an integral value, the rounded value is preserved
as long as it does not overflow. When an integral value is converted to a floating
value, the value is preserved unless a value of more than six significant digits is being
converted to single precision, or fifteen significant digits is being converted to double
precision.
Arithmetic Conversions
Many types of operations in C require two operands to be converted to a common
type. Two sets of conversion rules are applied to accomplish this conversion. The
first, referred to as the integral promotions, defines how integral types are promoted to
one of several integral types that are at least as large as int. The second, called the
usual arithmetic conversions, derives a common type in which the operation is
performed.
Integral Promotions
The difference between the ANSI C and traditional versions of the conversion rules is
that the traditional C rules emphasize preservation of the (un)signedness of a quantity,
while ANSI C rules emphasize preservation of its value.
In traditional C, operands of types char, unsigned char, and unsigned short are
converted to unsigned int. Operands of types signed char and short are converted
to int.
ANSI C converts all char and short operands, whether signed or unsigned, to int.
Only operands of type unsigned int , unsigned long, and unsigned long long may
remain unsigned.
The following subsections describe two sets of conversion rules, one for traditional C,
and the other for ANSI C. Each set is ordered in decreasing precedence. In any
particular case, the rule that applies is the first whose conditions are met.
Each rule specifies a type, referred to as the result type. Once a rule has been chosen,
each operand is converted to the result type, the operation is performed in that type,
and the result is of that type.
• If any operand is of type long double, the result type is long double.
• If any operand is of type double, the result type is double.
• If any operand is of type float, the result type is float.
• The integral promotions are performed on each operand as follows:
The (nonexistent) value of a void object cannot be used in any way, and neither
explicit nor implicit conversion can be applied. Because a void expression denotes a
nonexistent value, such an expression can be used only as an expression statement
An expression can be converted to type void by use of a cast. For example, this makes
explicit the discarding of the value of a function call used as an expression statement.
Conversion of Pointers
A pointer to void can be converted to a pointer to any object type and back without
change in the underlying value.
The NULL pointer constant can be specified either as the integral value zero, or the
value zero cast to a pointer to void. If a NULL pointer constant is assigned or
compared to a pointer to any type, it is appropriately converted.