Type conversion in C is done by the C Compiler according to some predefined rule of C language. The two types of implicit type conversions are automatic type conversion and type conversion in assignments.
Automatic unary conversions : All operands of type char and short are converted to int before any operation.
Automatic binary conversions: The rules of automatic binary conversion are as –
- If one operands is long double, then the other will be converted to long double, and the result will be long double.
- Otherwise if one operands is double, then the other will be converted to double and the result will be double.
- Otherwise if one operands is float, the other will be converted to float and the result will be float.
- Otherwise if one operands is unsigned long int, then other will be converted to unsigned long int and the result will be unsigned long int.
- Otherwise if one operands is long int and other is unsigned int.
- If long int can represent all the values of an unsigned long int and the result will be converted to long int an 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 long int, then the other will be converted to long int and the result will be long int.
- Other wise if one operands is unsigned int, the other will be converted to unsigned int and the result will be unsigned int.
- Otherwise both operands will be int and the result will be int.
If we leave aside unsigned variables, then these rules are rather simple and can be summarized by assigning a rank to each data type. whenever there are two operands of different data types the operands with a lower rank will be converted to the type of higher rank operand. The is called promotion of data type.
Type conversion in Assignment
If the types of the operands in an assignment expression are different, then the type of the right hand side operands is converted to the type of left hand operands. If the right hand operands is of lower rank then it will be promoted to the rank of left hand operand, and if it is of higher rank then it will demoted to the rank of left hand operand.
So consequences of these promotions and demotions are –
- Some high order bits may be dropped when long is converted to int, or int is converted to short int or char.
- Fraction part will be truncated during conversion of float type to int type.
- When double type is converted to float type, digit 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.
Explicit Type Conversion or Type Casting
There may be certain situation where implicit conversions may not solve our propose , For example-
int x=20, y=3;
The value of z will be 6.0 instead of 6.6.
In these type of cases we can specify our own conversion known as type casting or coercion. This is done with the help of cast operator. The cast operator is a unary operator that is used for converting an expression to particular data type temporarily . The syntax of cast operator is-
here the datatype along with the parenthesis is called the cast operator.
If we write the above statement as –
The value of z will come out to be 6.66. This happens because the cast operator (float) temporarily converted the int variable x into float type and so mixed mode arithmetic took place and fraction part was not lost.
Example : program to illustrate the use of cast operator?
Initially the expression x/y is evaluated, both x and y are integers so according to integer arithmetic after division, decimal value is truncated and result is integer value 2. This value will be assigned to p, but p is a float variable so according to p. so finally the value of p is 2.0.
When cast operator is used, mixed mode arithmetic is performed and thus the value is 2.5.