June 17, 2018

Type Conversion in C


   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
  1. If one operand is of type long double, then the other will be converted to long double, and the result will be long double.

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

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

  4. 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.

  5. 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.

  6. 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.

  7. 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.


Implicit Type Conversion



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 :

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

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

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

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

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

The following program demonstrates type conversion in assignment.

#include <stdio.h>

int main(void)
{

    char ch1, ch2;
    int i1, i2;
    float f1,f2;
    ch1 = 'A';
   
    /* float is demoted to int, only 70 is assigned to i1 */
    i1 = 70.56;
   
    f1 = 10.6;
   
    /* int is demoted to char */
    ch2 = i1;
   
    /* float is demoted to int, only 10 is assigned to i2 */
    i2 = f1;
   
    /* Now ch2 has the character with ASCII value 70, i2 is assigned value 10. */

    printf("ch2 = %c \n", ch2);
    printf("i2 = %d \n\n", i2);

    /* int is promoted to float */
    f2 = i1;
   
    /* char is promoted to int */
    i2 = ch1;
   
    /* Now i2 contains ASCII value of character 'A' which is 65 */
   
    printf("f2 = %.2f \n", f2);
    printf("i2 = %d \n", i2);

   
   return 0;
}


Output:

ch2 = F
i2 = 10

f2 = 70.00
i2 = 65



Explicit Type Conversion or Type Casting

We have just discussed how C performs type conversion automatically. However, there are instances when we want to force a type conversion in a way that is different from the automatic conversion. For example:

float z;
int x = 40, y = 3;
z = x/y;


The value of z will be 13.000000 instead of 13.333333 because operation between two integers yields an integer value. In these types of cases we can specify our own conversions 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 a particular data type temporarily. 

The syntax of cast operator is:

(datatype)expression

Here the datatype along with the parentheses is called the cast operator.

If we write the above statement as:

z = (float)x/y;

The value of z will come out to be 13.333333. This happens because the cast operator (float) temporarily converted the int variable x into float type and so mixed mode arithmetic took place and fractional part was not lost.

Note that the cast operator changes the data type of variable x only temporarily for the evaluation of this expression, everywhere else in the program it will be an int variable only.

The following program demonstrates this example.

#include <stdio.h>

int main(void)
{

    float z,r;
    int x = 40, y = 3;

    z = x / y;
    printf("z = %f\n", z);

    r = (float)x / y;
    printf("r = %f", r);

    return 0;
}


Output:

z = 13.000000
r = 13.333333



WARNING! In the program above, the following statement would still have resulted in integer division:

r = (float)(x / y);

The result of the expression x/y is 13. When 13 is converted to a float, it is 13.000000. To prevent the integer division from taking place, one of the operands should be converted to a float.




next    Decision Making in C
top    Index
prev    Operator Precedence and Associativity in C



No comments:

Post a Comment