Sams Teach Yourself C in 21 Days

(singke) #1
of errors can result when the proper conversion isn’t applied. The following sections
cover C’s automatic and explicit type conversions.

Automatic Type Conversions ........................................................................

As the name implies, automatic type conversions are performed automatically by the C
compiler without any action on your part. However, you should be aware of what’s going
on so that you can understand how C evaluates expressions.

566 Day 20

Note Automatic type conversion is often referred to as implicitconversions.


Type Promotion in Expressions
When a C expression is evaluated, the resulting value has a particular data type. If all the
components in the expression have the same type, the resulting type is that type as well.
For example, if xandyare both type int, the following expression is type intalso:
x + y
What if the components of an expression have different types? In that case, the expres-
sion has the same type as its most comprehensive component. From least-comprehensive
to most-comprehensive, the numerical data types are
char
short
int
long
long long
float
double
long double

Thus, an expression containing an intand a charevaluates to type int, an expression
containing a longand a floatevaluates to type float, and so on.
When creating expressions, the compiler uses two variables or values at a time. For
example, if you have the expression:
Y + X * 2

32 448201x-CH20 8/13/02 11:16 AM Page 566

Free download pdf