Programming in C

(Barry) #1
5.0 Expressions 451

Types other than structures can be defined as well, for example, if intPtris of type
int *, the statement
intPtr = (int [100]) {[0] = 1, [50] = 50, [99] = 99 };
(which can appear anywhere in the program) sets intptrpointing to an array of 100
integers, whose three elements are initialized as specified.
If the size of the array is not specified, it is determined by the initializer list.

5.17 Conversion of Basic Data Types


The C language converts operands in arithmetic expressions in a predefined order,
known as the usual arithmetic conversions.
Step 1: If either operand is of type long double, the other is converted to
long double, and that is the type of the result.
Step 2: If either operand is of type double, the other is converted to double,
and that is the type of the result.
Step 3: If either operand is of type float, the other is converted to float, and
that is the type of the result.
Step 4: If either operand is of type _Bool,char,short int,intbit field, or of
an enumerated data type, it is converted to int, if an intcan fully
represent its range of values; otherwise, it is converted to unsigned
int. If both operands are of the same type, that is the type of the
result.
Step 5: If both operands are signed or both are unsigned, the smaller integer
type is converted to the larger integer type, and that is the type of the
result.
Step 6: If the unsigned operand is equal in size or larger than the signed
operand, then the signed operand is converted to the type of the
unsigned operand, and that is the type of the result.
Step 7: If the signed operand can represent all of the values in the unsigned
operand, the latter is converted to the type of the former, and that is
the type of the result.
Step 8: If this step is reached, both operands are converted to the unsigned
type corresponding to the type of the signed type.
Step 4 is known more formally as integral promotion.
Conversion of operands is well behaved in most situations, although the following
points should be noted:


  1. Conversion of a charto an intmight involve sign extension on some machines,
    unless the charis declared as unsigned.


20 0672326663 AppA 6/10/04 2:01 PM Page 451

Free download pdf