# Type conversion C

An arithmetic operation between two integers gives an integer result. Operation between two real numbers gives a real result. Let us turn our attention to cases when the operands in an expression are of different types. The general rule is that if a binary operator has operands of different types, then the ‘lower’ type is promoted to the ‘higher’ type, before the operation proceeds. If either operand is long double convert other to long double and result is long double. If one operand is integer and other float, integer is converted to float and result is float and so on.

E.g Result
5 / 2. 0 = 5.0 / 2.0 = 2.5
2.0 / 5 = 2.0 /5.0 = 0.4
2 / 5 = 2 / 5 = 0
5 / 2 = 5 / 2 = 2
5.0 / 2.0 = 5.0 / 2.0 = 2.5
Further if type of expression on the right of ‘=’ and type of variable on the left differ, then the value of expression is promoted or demoted depending upon the type of variable on left side of ‘=’,

E.g.

int j ; float b ; j = 3.5 ; b = 30 ;

Here though 3.5 is a real number. When it is assigned to j (which is an integer), it is converted to an integer i. e. j becomes 3. Conversion to an integer truncates the decimal part. Also 30 assigned to b convert it to floating point value 30.000. Since b is a float variable.
In the following expression.

/ * variable declaration * / float a, c ; int s ; s = a * b = c * / 100 + 32 / 4 – 3 * 1 ; / * Expression * /

Here since the expression on the right is a mixed type, whenever an operator has different types of operands, they are converted according to above given rule and then expression solved. Whatever will be the resulting type of expression on right it is finally truncated to integer value and assigned to s. Since s is of integer type.