When variables and constants of different types are combined in an expression then they are converted to same data type. The process of converting one predefined type into another is called Type conversion
Type conversion in c can be classified into the following two types:
When the Type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion.
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given below:
All short and char are automatically converted to int, then,
It should be noted that the final result of expression is converted to type of variable on left side of assignment operator before assigning value to it.
Also, conversion of float to int causes truncation of fractional part, conversion of double to float causes rounding of digits and the conversion of long int to int causes dropping of excess higher order bits.
The Type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion.
The explicit type conversion is also known as type casting.
Type casting in c is done in the following form:
where, data_type is any valid c data type, and expression may be constant, variable or expression.
The following rules have to be followed while converting the expression from one type to another to avoid the loss of information: