Expert C Programming

(Jeff_L) #1

conversions" changed between ye olde originale K&R C and ANSI C. Thus, where Kernighan and
Ritchie say something like:


Section 6.6 Arithmetic Conversions


A great many operators cause conversions and yield result types in a similar way. This pattern will be
called the "usual arithmetic conversions."


First, any operands of type char or short are converted to int, and any of type float are converted to
double. Then if either operand is double, the other is converted to double and that is the type of the
result. Otherwise, if either operand is long, the other is converted to long and that is the type of the
result. Otherwise, if either operand is unsigned, the other is converted to unsigned and that is the type
of the result. Otherwise, both operands must be int, and that is the type of the result.


The ANSI C manual has closed the loopholes by rewriting this as:


Section 6.2.1.1 Characters and Integers (the integral promotions)


A char, a short int, or an int bit-field, or their signed or unsigned varieties, or an enumeration type,
may be used in an expression wherever an int or unsigned int may be used. If an int can represent all
the values of the original type, the value is converted to an int; otherwise it is converted to an unsigned
int. These are called the integral promotions.


Section 6.2.1.5 Usual Arithmetic Conversions


Many binary operators that expect operands of arithmetic type cause conversions and yield result
types in a similar way. The purpose is to yield a common type, which is also the type of the result.
This pattern is called the "usual arithmetic conversions."


First, if either operand has 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
[refer to section 6.2.1.1 for the integral promotions] are performed on both operands. Then the
following 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, if a long int can
represent all values of an unsigned int the operand of type unsigned int is converted to long int; if a
long int cannot represent all the values of an 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.


The values of floating operands and of the results of floating expressions may be represented in
greater precision and range than that required by the type; the types are not changed thereby.


In English (complete with loopholes and lack of precision), the ANSI C version would mean
something like:


Operands with different types get converted when you do arithmetic. Everything is converted to the
type of the floatiest, longest operand, signed if possible without losing bits.

Free download pdf