C permits mixing of constants and variables of different types in an expression. In these types of operations, data type of one operand is converted into data type of another operand to get a uniform type. This is known as type conversion.



There are two types of type conversion:

Implicit type conversion.

Explicit type conversion.

Implicit type conversions are done by the compiler while the explicit type conversions are user defined conversions.

Implicit Type Conversion

Implicit type conversion is an automatic type conversion by the compiler. The type conversion is carried out when the expression contains different types of data items. When the compiler carries such type conversion itself by using inbuilt data types in routines then it is called implicit type conversion.

This type of conversion is done by the compiler according to the following rules:

First, all operands of type char and short are converted to int , then

If one operand is of type long double , then the other will be converted to long double , and the result will be long double .

Otherwise if one operand is of type double , then the other will be converted to double and the result will be double .

Otherwise if one operand is of type float , the other will be converted to float and the result will be float .

Otherwise if one operand is of type unsigned long int , then other will be converted to unsigned long int and the result will be unsigned long int .

Otherwise if one operand is of type long int and other is of type unsigned int

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 .

Else both the operands will be converted to unsigned long int and the result will be unsigned long int .

Otherwise if one operand is of type long int , then the other will be converted to long int and the result will be long int .

Otherwise if one operand is of type unsigned int , then the other will be converted to unsigned int and the result will be unsigned int .

Let's take some examples:

Example 1:

char ch = 'A' ;

int a = 5 ;



a + ch;

Here, char will be converted to int before any operation and the result of the overall expression will be of type int . Since the integral value of ch is 65 (i.e., ASCII value of the character 'A' ). Hence 65 + 5 = 70 .

The following program demonstrates this example.

#include <stdio.h>



int main( void )

{



char ch = 'A' ;

int a = 5 ;





printf ( "'A' + 5 = %d" , a+ch);



return 0;

}

Output:

'A' + 5 = 70

Example 2:

int a = 10 ;

float b = 3.5 ;



a + b;

Here, int will be converted to float before any operation and the result of the overall expression will be of type float . Hence the result of the overall operation is float i.e. 13.500000 .

The following program demonstrates this example.

#include <stdio.h>



int main( void )

{



int a = 10 ;

float b = 3.5 ;





printf ( "a + b = %f" , a+b);



return 0;

}

Output:

a + b = 13.500000

Example 3:

int i = 90 ;

float f = 3.5 ;

char c = 'A' ;

double d = 2.3456 ;



double result = (f*i) + (i/c) - (d*i);

Here, in the first subexpression, f*i , i is converted to a float and the result of the subexpression is float . Next, in the subexpression i/c , c is converted to int , and the result is of type int . Then, in d*i , the value of i is converted to double , and the type of the expression is double . Finally, these three intermediate values, float , int , and double , are considered. The outcome of float plus an int is a float . Then the resultant float minus the last double is converted to double , which is the type for the final result of the expression.

The following program demonstrates this example.

#include <stdio.h>



int main( void )

{



int i = 90 ;

float f = 3.5 ;

char c = 'A' ;

double d = 2.3456 ;



double result = (f*i) + (i/c) - (d*i);





printf ( "result = %f" , result);



return 0;

}

Output:

result = 104.896000

All the above-mentioned rules can be simplified by assigning a rank to each data type. When the expression contains different types of data items, the operand with a lower rank will be converted to the type of higher rank operand. This is called promotion of data type.

Type Conversion in Assignment

If the types of the two operands in an assignment expression are different, then the type of the right hand side operand is converted to the type of left hand operand according to the following rules.

Rule 1: If the right hand operand is of lower rank then it will be promoted to the rank of left hand operand.

For example,

float b;

b = 30 ;



Here right operand 30 is of type int and the left-hand operand is of type float . According to rule lower rank operand (in this case int ) will be promoted to a higher rank (in this case float ). So before assignment 30 will be promoted to float and then assigned to b . Hence what gets stored in b is 30.000000 .

Rule 2: Otherwise, if right-hand operand is of higher rank then it will be demoted to the rank of left-hand operand.

For example,

int i;

i = 3.5 ;



Here right operand 3.5 is of type float and the left-hand operand is of type int . According to rule higher rank operand (in this case float ) will be demoted to the lower rank (in this case int ). So before assignment 3.5 will be demoted to int and then assigned to i . Hence what gets stored in i is 3 .

Some consequences of these promotions and demotions are :

Some high order bits may be dropped when long int is converted to int , or int is converted to short int or char .

Fractional part will be truncated during conversion of float type to int type.

When double type is converted to float type, digits are rounded off.

When a signed type is changed to unsigned type, the sign may be dropped.

When an int is converted to float , or float to double there will be no increase in accuracy or precision.