Share this post on:

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 Conversions

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.
  1. 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.
  2. 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 –

  1. Some high order bits may be dropped when long is converted to int, or int is converted to short int or char.
  2. Fraction part will be truncated during conversion of float type to int type.
  3. When double type is converted to float type, digit 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.

Explicit Type Conversion or Type Casting

There may be certain situation where implicit conversions may not solve our propose , For example-

float z;

int x=20, y=3;

z=x/y;

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-

(datatype) expression

here the datatype along with the parenthesis 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 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?

#include<stdio.h>

int main(void)

{

int x=5,y=2;

float p,q;

p=x/y;

printf(“p=%f\n”,p);

q=(float)x/y;

printf(“q=%f\n”,q);

return 0;

}

Output :

p=2.000000

q=2.500000

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.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *