Explain Implicit type conversion in C Language while evaluating expressions with examples


There are two types of castings or conversions

  1. Implicity Casting
  2. Explicit Casting

Implicity Casting

C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without losing any significance. This automatica conversion is known as implicit type conversion.

When the data type of smaller size is converted into higher size, implicit casting is done. It will be done by the compiler itself. If the operands are of different types, the lower type is automatically converted to higher types and the result will be of higher type. The result of expression is then converted to the type of variable at left hand side of the assignment operator.

Rules for implicit type conversion.

  • All short and char are automatically converted in to int.
  • If one of the operands is long double, the other will be converted to long double and the result will be long double.
  • else, if one of the operands is double, the other will be converted to double and the result will be double.
  • else, if one of the operands is float, the other will be concerted to float and the result will be float.
  • else, if one of the operands is long int and the other is unsigned int, then
    1. 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.
    2. else, both operands will be converted to unsigned long in and the result will be unsigned long int.
  • else, if one of the operands is long int, the other will be converted to long int and the result will be long int.
  • else, if one of the operands is unsigned int, the other will be converted to unsigned int and the result will be unsigned int.

Example - 1

 int i;
 char c = '5';
 i = c;

This process is also called widening, because here the lower data type value is assigned to higher data type.

Example - 2

 int i;
 float f;
 double d;
 int j = i * d + f/i;

In the given expression, j = i * d + f/i; the evaluation is done as follows.

* and / have higher precedence, so i * d and f / i are evaluated first.

  • In sub expression i * d, i is of type integer and d is of type double. So i is converted to double data type (since, double is having higher precedence than integer)
  • In sub expression f / i, f is of type float and i is of type integer. So i is converted to float data type.
    So, now j = double data type + float data type.
  • In this expression, float data type is converted to double data type(Since, double is having higher precedence than float).
  • In this assignment statement, j is of type integer so the double type data is converted to integer type (the type of LHS of assignment Operator).
0 Komentar untuk "Explain Implicit type conversion in C Language while evaluating expressions with examples"

Back To Top