• Before execution of an operation takes place, both
the operands must have the same data type.
• If the operands are of different data type, C converts
one or both the operands to the appropriate data
types by type conversion.
Classification of type conversion
Type Conversion
Implicit Explicit
Automatic Assignment
Implicit type conversion of one data type to
another type is performed automatically as
per the rules described in the C language.
Two types
◦ Automatic type conversion
◦ Assignment type conversion
Automatic type conversion
◦ The data type having lower rank is converted
automatically into higher rank data before the
operation proceeds.
Automatic type conversion
◦ The data type having lower rank is converted
automatically into higher rank data before the
operation proceeds.
Ranking table
Data Type Rank
long 1 (Higher rank)
double 2
double 3
float 4
int 5
short int 6 (Lower rank)
char
• Automatic type conversion rules
– If one of the operands is long double, double or float,
the other operand is converted to long double, double
or float respectively.
– Otherwise, if either operand is unsigned long, the
other is converted to unsigned long.
– Otherwise, if one operand is long and the other is
unsigned, the unsigned is converted to long provided
the long type can represent the whole range of values
that an unsigned might possess. If the long can not
represent some unsigned values, then both the
operands are converted to unsigned long.
◦ Otherwise, if either operand is long, the other
operand is converted to long.
◦ Otherwise, if either operand is unsigned, the other
operand is converted to unsigned.
◦ Otherwise, char and short are converted to int.
◦ Otherwise, both operands are of int type.
Assignment type conversion
◦ If the two operands in an assignment operation are of
different data types, the right side operand is automatically
converted to the data type of the left side.
◦ Whenever the type of the left operand has lower rank than
the type of the right operand, the result of the assignment
is less precise than the actual value.
Downward type conversion
Left operand Right operand Conversion rule
type type
float double Truncate/Round and truncate
(Implementation defined)
long float Truncate fraction
int long Truncate higher order bit
int float Truncate fraction
short int Truncate higher order bits
char short Truncate
Scalar data types can be explicitly converted into
desired data types.
• Format: (datatype) expression
where, expression is converted to the
datatype specified in paranthesis.
Examples:
◦ (float)1 converts the integer constant 1 to the float
value 1.0. Hence, (float)1/3 will be evaluated as
1.0/3 yielding 0.333333, whereas 1/3 yields 0.
◦ (long double)(10+25) converts the integer constant
35 to long double.
1) #include<stdio.h>
main()
{
int a=5;
float b=a;
printf(“%f”,b);
}
2) #include<stdio.h>
main()
{
int x=97;
printf(“%c”,x);
}
#include<stdio.h>
main()
{
int a=5,b=8;
float c=0,d=0;
c=a/b;
printf(“%f”,c);
d=(float)a/(float)b;
printf(“%f”,d);
}