Implicit conversions
When constants and variables of different types are mixed
in the same expression, C compiler will convert them to the same type
automatically (``implicit conversion'')
unless they are already casted to the same type.
The following topics contain more details about implicit conversions:
Characters and integers
Any of the following may be used
in an expression where an int or
unsigned int may be used.
-
char
-
short int
-
A char, short, or int bit-field
-
The signed or unsigned varieties of any of the above types
-
An object or bit-field that has enumeration type
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.
This process is called integral promotion.
Compilation mode dependencies that affect unsigned types
-
Under compilation mode -Xk or -Xt, unsigned char
and unsigned short are promoted to unsigned int.
-
Under compilation mode -Xa, -Xc, or -Xm,
unsigned char and unsigned short
are promoted to int.
Signed and unsigned integers
-
When an integer is converted to another
integral type, the value is unchanged if the value can
be represented by the new type.
-
If a negative signed integer is converted to an
unsigned integer with greater size,
the signed integer is first promoted to the signed
integer corresponding to the unsigned integer.
Integral and floating
When a floating type is converted to any integral
type, any fractional part is discarded.
Float and double
A float is promoted to double
or long double,
or a double is promoted to long double
without a change in value.
The actual rounding behavior that is used when a floating point
value is converted to a smaller floating point value depends
on the rounding mode in effect at the
time of execution.
The default rounding mode is ``round to nearest.''
See ``Floating Point Operations'' and
the IEEE Standard for Binary Floating-Point Arithmetic
(ANSI/IEEE Std 754-1985) for a more complete discussion
of rounding modes.
Usual arithmetic conversions
Some binary operators convert the types
of their operands in order to yield a common type,
which is also the type of the result.
These are called the usual arithmetic conversions:
-
If either operand is 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, these 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,
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.
Next topic:
Expressions
Previous topic:
Conversions and expressions
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003