Chapter 3 C Programming
Chapter 3 C Programming
(1) (2)
6
(3)
5
(4)
11
(5)
10
TYPE CONVERSION IN
EXPRESSIONS
►C permits the mixing of constants and
variables of different types in an expression.
► If the operands are of different types, the
lower type is automatically converted to
higher type before the operation proceeds.
The result is of the higher type. This
automatic conversion is known as implicit
type conversion.
Rules for Conversion
► All short and char are automatically converted to
int;
► If one of the operands is long double, the other
will be converted to long double and the result will
be in long double.
► Else, If one of the operands is double, the other
will be converted to double and the result will be
in double.
► Else, If one of the operands is float, the other will
be converted to float and the result will be in float.
Contd..
► Else, If one of the operands is unsigned long int,
the other will be converted to unsigned long int
and the result will be in unsigned long int.
► Else, if one of the operands is long int and other is
unsigned int, then:
▪ (a) if unsigned int can be converted to long int, the
unsigned int operand will be converted as such and the
result will be long int.
▪ (b) else, both the operands will be converted to
unsigned long int and the result will be unsigned long
int.
Contd..
► Else,If one of the operands is long int, the
other will be converted to long int and the
result will be in long int.
► Else, If one of the operands is unsigned int,
the other will be converted to unsigned int
and the result will be in unsigned int.
Casting a Value
► C performs type conversion automatically. However, there
are instances when we want to force a type conversion in
a way that is different from the automatic conversion.
▪ Eg: 1) ratio = female_number / male_number